Java 在JButton单击上显示JLabel

Java 在JButton单击上显示JLabel,java,swing,jbutton,jlabel,Java,Swing,Jbutton,Jlabel,我想在单击我的showJButton时查看Jlabel,但它不起作用 public class d5 extends JFrame implements ActionListener { JButton showButton; static JLabel[] lbl; JPanel panel; public d5() { showButton = new JButton("Show"); showButton.addActi

我想在单击我的show
JButton
时查看
Jlabel
,但它不起作用

public class d5 extends JFrame implements ActionListener {

    JButton showButton;
    static JLabel[] lbl;
    JPanel panel;

    public d5() {

        showButton = new JButton("Show");
        showButton.addActionListener(this);
        add(showButton, BorderLayout.PAGE_START);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 500);
        setLocation(300, 30);
        setVisible(true);
    }

    public JPanel mypanel() {
        panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        lbl = recordsLabel();
        for (JLabel jLabel : lbl) {
            panel.add(jLabel);
        }
        return panel;
    }

    public static void main(String[] args) {
        new d5();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == showButton) {
            add(mypanel(), BorderLayout.PAGE_START);
            setVisible(true);
            System.out.println("show button clicked");
        }
    }

    public JLabel[] recordsLabel() {
        ArrayList<String> lableList = new ArrayList<>();
        lableList.add("one");
        lableList.add("two");
        lableList.add("three");
        Object[] arrayResultRow = lableList.toArray();

        int rows = 3;

        lbl = new JLabel[rows];
        for (int i = 0; i < rows; i++) {
            lbl[i] = new JLabel(arrayResultRow[i].toString());
        }
        return lbl;
    }
}
公共类d5扩展JFrame实现ActionListener{
JButton显示按钮;
静态JLabel[]lbl;
JPanel小组;
公共d5(){
showButton=新的JButton(“Show”);
showButton.addActionListener(此);
添加(showButton,BorderLayout.PAGE_START);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置大小(400500);
设定位置(300,30);
setVisible(真);
}
公共JPanel mypanel(){
panel=newjpanel(newflowlayout(FlowLayout.LEFT));
lbl=recordsLabel();
用于(JLabel JLabel:lbl){
面板。添加(jLabel);
}
返回面板;
}
公共静态void main(字符串[]args){
新d5();
}
@凌驾
已执行的公共无效操作(操作事件e){
如果(例如getSource()==showButton){
添加(mypanel(),BorderLayout.PAGE_START);
setVisible(真);
System.out.println(“单击显示按钮”);
}
}
公共JLabel[]记录标签(){
ArrayList lableList=新的ArrayList();
标签列表。添加(“一”);
标签列表。添加(“两个”);
标签列表。添加(“三”);
对象[]arrayResultRow=lableList.toArray();
int行=3;
lbl=新的JLabel[行];
对于(int i=0;i
作为@nicecow注释,您已经添加了
(显示按钮,BorderLayout.PAGE\u START)与面板位于同一位置。只能在同一位置添加一个组件

另外,调用validate也不错

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == showButton) {
        add(mypanel(), BorderLayout.PAGE_START); // set another position or remove previous component here
        validate(); 
        System.out.println("show button clicked");

    }
}
顺便说一下,我不建议在
JFrame
类中实现
ActionListener
,也不需要扩展
JFrame

public class D5 { 

private JFrame frame;

.
. // is some part in constrcutor
.
  showButton.addActionListener(new ActionListener(){
        @Override 
         public void actionPerformed(ActionEvent evt){
              frame.add(mypanel(),BorderLayout.PAGE_START);
              frame.validate();
         }
  })
}

你期待什么?你得到了什么?错误是什么?它在哪里?在任何给定的时间,只有一个组件可以添加到任何给定的位置,要么观察代码,要么在同一位置添加两个组件
add(showButton,BorderLayout.PAGE_START)和在您的
actionPerformed()
方法
add(mypanel(),BorderLayout.PAGE_START)中
@marounnaroun问题已解决,谢谢…问题出现在
BorderLayout.PAGE\u START
部分,我应该在另一
BorderLayout
部分定义另一个组件,但我在
BorderLayout.PAGE\u START
模式中定义了其中两个组件!