java frame类,下面程序怎么添加背景,改变按钮位置啊,大神帮帮
importjava.awt.FlowLayout;importjava.awt.event.ActionEvent;importjava.awt.event.Actio...
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;
public class C extends JFrame implements ActionListener {
JPanel bPanel;
JButton a;
JButton b;
JButton c;
public C() {
a = new JButton("时钟");
b = new JButton("计算器");
c = new JButton("贪吃蛇");
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
bPanel = new JPanel();
bPanel.add(a);
bPanel.add(b);
bPanel.add(c);
this.setContentPane(bPanel);
this.setTitle("小程序");
this.setBounds(300, 200, 400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
System.out.println("source: " + e.getSource());
String name = ((JButton) (e.getSource())).getText();
System.out.println("name: " + name);
if (name.equals("时钟")) {
new Clock();;
} else if (name.equals("计算器")) {
new JCalculator();;
} else if (name.equals("贪吃蛇")) {
new ShellMain();
}
}
public static void main(String[] args) {
new C();
}
}
就像这样 展开
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class C extends JFrame implements ActionListener {
JPanel bPanel;
JButton a;
JButton b;
JButton c;
public C() {
a = new JButton("时钟");
b = new JButton("计算器");
c = new JButton("贪吃蛇");
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
bPanel = new JPanel();
bPanel.add(a);
bPanel.add(b);
bPanel.add(c);
this.setContentPane(bPanel);
this.setTitle("小程序");
this.setBounds(300, 200, 400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
System.out.println("source: " + e.getSource());
String name = ((JButton) (e.getSource())).getText();
System.out.println("name: " + name);
if (name.equals("时钟")) {
new Clock();;
} else if (name.equals("计算器")) {
new JCalculator();;
} else if (name.equals("贪吃蛇")) {
new ShellMain();
}
}
public static void main(String[] args) {
new C();
}
}
就像这样 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询