私のコードを見てください。欲しいのはうまくいきますが、唯一の問題は現在のボタンの反対側に別のボタンを追加したいということです。私はそうすることができません私の体は私を助けてください。私のコード全体は完成しましたが、2番目のボタンを追加できません
それは間違って行くを開始どこでimport java.awt.event.*;
import javax.swing.*;
public class Example2 extends JFrame {
public Example2() {
initUI();
}
public final void initUI() {
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
panel.setToolTipText("A Panel container");
JButton button = new JButton("Even");
button.setBounds(100, 60, 100, 30);
button.setToolTipText("A button component");
JButton button2 = new JButton("Odd");
button2.setBounds(100, 60, 100, 30);
button2.setToolTipText("A button component");
//Add action listener to button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System .out.println("You clicked the button");
int sum=0;
for(int i=1;i<=100;i++){
if(i%2==0){
System.out.println(i);
sum+=i;
}
}
System.out.println("Sum of even numbers: "+sum);
}
});
panel.add(button);
panel.add(button2);
setTitle("Tooltip");
setSize(500, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Example2 ex = new Example2();
ex.setVisible(true);
}
}
可能重複(http://stackoverflow.com/questions/9961713/display-another-button-and-print-even-numbers-when [別のボタン及び印刷ボタンを押しても数字を表示します] -Button-pressed) – Robin