簡體   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