繁体   English   中英

我不知道如何使用 JButton Action

[英]I can't figure out how to use JButton Action

您好,我是 Java 的新手。 我不知道如何使用 JButton Action。 嗨,我是 JAVA 的新手,我需要一些帮助。 我的训练任务的目标是创建一个带有按钮的表格,该按钮还可以创建带有数据的表格,然后组合选定的数据。 但我可能面临一个非常简单的问题,我不知道如何在按钮上设置动作,或者更确切地说如何使按钮只绘制一个 class。 我的代码如下。

这是我的主要方法:

public class Test {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    JFJB JaFr = new JFJB();
    JaFr.JFJB();
}  

}

JFrame+JButton:

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

 public class JFJB extends JFrame {

// JPanel/Frame
JPanel pnlFrameforButton = new JPanel();
// JButton
JButton btnMakeTable = new JButton("MakeTable");




public void JFJB() {       
   
    
    // FlightInfo setbounds
    btnMakeTable.setBounds(60, 400, 220, 30);

    // JPanel bounds
    //pnlButton.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));
    pnlFrameforButton.setBounds(0, 0, 50, 50);
    

    // Adding to JFrame
    pnlFrameforButton.add(btnMakeTable);
    add(pnlFrameforButton);

    // JFrame properties
    setSize(350, 200);
    setBackground(Color.BLACK);
    setTitle("Just Frame with Button");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

}

我要画的class:

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;


 public class PlayerTable extends JFrame {

 public static void createPlayerTable() {
      JFrame frame = new JFrame("Test frame");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      String[] columnNames = {
                "Name",
                "Score"
      };
       
      
      String[][] data = {
                {"addins", "02.11.2006 19:15", "Folder", ""},
                {"AppPatch", "03.10.2006 14:10", "Folder", ""},
                {"assembly", "02.11.2006 14:20", "Folder", ""},
                {"Boot", "13.10.2007 10:46", "Folder", ""},
                {"Branding", "13.10.2007 12:10", "Folder", ""},
                {"Cursors", "23.09.2006 16:34", "Folder", ""},
                {"Debug", "07.12.2006 17:45", "Folder", ""},
                {"Fonts", "03.10.2006 14:08", "Folder", ""},
                {"Help", "08.11.2006 18:23", "Folder", ""},
                {"explorer.exe", "18.10.2006 14:13", "File", "2,93MB"},
                {"helppane.exe", "22.08.2006 11:39", "File", "4,58MB"},
                {"twunk.exe", "19.08.2007 10:37", "File", "1,08MB"},
                {"nsreg.exe", "07.08.2007 11:14", "File", "2,10MB"},
                {"avisp.exe", "17.12.2007 16:58", "File", "12,67MB"},
      };
       
      JTable table = new JTable(data, columnNames);
       
      JScrollPane scrollPane = new JScrollPane(table);
       
      frame.getContentPane().add(scrollPane);
      frame.setPreferredSize(new Dimension(450, 200));
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
 }

}

btnMakeTable.addActionListener(e -> {
   PlayerTable.createPlayerTable();
});

同样在您的主要方法中,您不需要第二行,因为已经调用了构造函数并且它不是 static

您还可以实现接口 actionlistener 并使用 addActionListener(this);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM