0
所以我創建了兩個按鈕,我想單擊按鈕時執行特定的任務。如果使用ActionListener單擊按鈕1(b1),我想創建一個Van對象並在JTextarea或JTable中顯示實例變量。例如,如果單擊Van按鈕,則該操作將創建Van的對象並獲取實例變量值並將其打印在JTextArea/JTable中。下面是我到目前爲止的代碼:我如何顯示在JTextarea或JTable
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
public class TestButton extends JFrame{
JTable table;
public TestButton(){
setLayout(new FlowLayout());
}
static class ActionTwo implements ActionListener{
@Override
public void actionPerformed (ActionEvent evt){
Vehicle sport = new Sportcar (200, 1500, 220);
}
}
static class Action implements ActionListener{
@Override
public void actionPerformed (ActionEvent evt){
Vehicle aVan = new Van(100,0.9,3500,160.4);
}
}
public static void main (String [] args){
JFrame frame = new JFrame ("Type of Vehicle");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setBackground(Color.black);
JButton b1 = new JButton("Van");
JButton b2 = new JButton("Sports Car");
panel.add(b1);
panel.add(b2);
frame.add(panel);
b1.addActionListener(new Action());
b2.addActionListener(new ActionTwo());
}
}
謝謝兄弟,肯定會讀它。我討厭不理解正在發生的事情,但我確實得到了你所做的大部分工作,它看起來非常有條理。我喜歡它,並且會接受它! –