gpt4 book ai didi

java - 在面板中显示 ArrayList

转载 作者:行者123 更新时间:2023-11-29 06:12:40 27 4
gpt4 key购买 nike

我在下面的代码中遇到了这个问题,因为我希望在窗口上显示的 ArrayList 名称和值都没有出现。它应该显示在窗口的底部,但我将所有可能的设置设置为 setVisible true 但仍然无法显示它。我认为这是一个小错误,但我看不到它是我的代码。很抱歉,代码如此困惑,因为我是为了自己的理解而写的。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;


public class SamplePaper5a {

static final int width = 500;
static final int hight = 600;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

JFrame frame = new JFrame("Exam");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(width,hight);
Container contain = new Container();
contain = frame.getContentPane();

contain.setLayout(new GridLayout(2,2));

final ArrayList<String>names = new ArrayList<String>();
final ArrayList<String>values = new ArrayList<String>();

names.add("dian");
names.add("maze");
names.add("carl");
names.add("John");
names.add("tan");
names.add("james");

values.add("11111");
values.add("2222");
values.add("3333");
values.add("4444");
values.add("5555");
values.add("6666");

final JTextArea txtArea = new JTextArea();
JButton butt = new JButton(" Enter ");
contain.add(txtArea);
contain.add(butt);

butt.addActionListener(new ActionListener()
{

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
for(int i=0;i<values.size();i++)
{
txtArea.setText( txtArea.getText()+" \n "+values.get(i) +" "+ names.get(i));
}
}
});

try
{
contain.add(new NamePanel2(names,values,1));

}

catch(Size2Exception e)
{
System.out.print(e);
}

frame.setVisible(true);
}
}

class NamePanel2 extends JPanel
{
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> values = new ArrayList<String>();
int cols;

public NamePanel2(ArrayList<String> nam,ArrayList<String> val,int c) throws Size2Exception
{
this.names = nam;
this.values =val;
this.cols = c;
JPanel panel = new JPanel();

if(names.size()!= values.size())
throw new Size2Exception(" wrong sizes");

panel.setLayout(new GridLayout(names.size(),cols));

for(int i = 0; i<names.size();i++)
{
panel.add(new JLabel(names.get(i)));
panel.add(new JTextField(values.get(i)));
}
panel.setVisible(true);
}
}

class Size2Exception extends Exception
{
public Size2Exception(String str)
{
super(str);
}
}

最佳答案

有关修复的性质和一些提示,请参阅评论。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;


public class SamplePaper5a {

static final int width = 500;
static final int hight = 600;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

JFrame frame = new JFrame("Exam");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(width,hight);
// don't mix Swing with AWT
JPanel contain = new JPanel();
System.out.println(contain.getLayout());
frame.setContentPane(contain);

contain.setLayout(new GridLayout(2,2));

final ArrayList<String>names = new ArrayList<String>();
final ArrayList<String>values = new ArrayList<String>();

names.add("dian");
names.add("maze");
names.add("carl");
names.add("John");
names.add("tan");
names.add("james");

values.add("11111");
values.add("2222");
values.add("3333");
values.add("4444");
values.add("5555");
values.add("6666");

final JTextArea txtArea = new JTextArea();
JButton butt = new JButton(" Enter ");
contain.add(txtArea);
contain.add(butt);

butt.addActionListener(new ActionListener()
{

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
for(int i=0;i<values.size();i++)
{
txtArea.setText( txtArea.getText()+" \n "+values.get(i) +" "+ names.get(i));
}
}
});

try
{
contain.add(new NamePanel2(names,values,1));

}

catch(Size2Exception e)
{
System.out.print(e);
}

frame.setVisible(true);
}
}

class NamePanel2 extends JPanel
{
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> values = new ArrayList<String>();
int cols;

public NamePanel2(ArrayList<String> nam,ArrayList<String> val,int c) throws Size2Exception
{
this.names = nam;
this.values =val;
this.cols = c;
// unnecessary
//JPanel panel = new JPanel();

// it would be better to define an Object that contains a name/value pair
if(names.size()!= values.size())
throw new Size2Exception(" wrong sizes");

setLayout(new GridLayout(names.size(),cols));

for(int i = 0; i<names.size();i++)
{
add(new JLabel(names.get(i)));
add(new JTextField(values.get(i)));
}
// unnecessary
//panel.setVisible(true);
}
}

class Size2Exception extends Exception
{
public Size2Exception(String str)
{
super(str);
}
}

屏幕截图

enter image description here

顺便说一句——GUI 看起来很奇怪。我会把它留给你解决。

关于java - 在面板中显示 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6241970/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com