1.简单的窗体
import java.io.*;
import javax.swing.*;
public class try1 {
private static void createAndShowGUI() {
// 创建及设置窗口
JFrame frame = new JFrame("标题");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加 "Hello World" 标签
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
// 显示窗口
frame.pack();//调整此窗口的大小,以适合其子组件的首选大小和布局
frame.setVisible(true);
}
public static void main(String[] args) {
// 显示应用 GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
2.简单的 button 监听
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class c {
public static void main(String[] args) {
JFrame f = new JFrame("Simple Swing Application");
JButton button = new JButton("Click me!");
/**
* 该block增加鼠标监听事件
*/
button.addActionListener(new ActionListener(){ // 事件源(注册事件)
@Override
public void actionPerformed(ActionEvent e) { // 监听
button.setText("I've been clicked!");
}
});
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.getContentPane().add(button);
f.setSize(300, 300);
f.setVisible(true);
}
}
换一种写法
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class c {
public static void main(String[] args) {
MainFrame();
}
private static void MainFrame ()
{
JFrame f = new JFrame("窗体名字");
JButton button = new JButton("Click me!");
button.addActionListener(new ActionListener(){ // 事件源(注册事件)
@Override
public void actionPerformed(ActionEvent e) { // 监听
button.setText("I've been clicked!");
}
});
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.getContentPane().add(button);
f.setSize(300, 300);
f.setVisible(true);
}
}
3.弹窗
import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Login extends Frame{
public Login(String str){
super(str);
}
static Login fr = new Login("测试Panel");
public static void main(String[] args){
fr.setSize(500, 500);
fr.setLocation(500, 300);
fr.setBackground(null);
fr.setLayout(null);
Button button = new Button("点击我");
button.setSize(50, 25);
//button.setBorderPainted(false);
button.setLocation(50, 50);
button.addActionListener(new ActionListener(){
//单击按钮执行的方法
public void actionPerformed(ActionEvent e) {
closeThis();
//创建新的窗口
JFrame frame = new JFrame("新窗口");
//设置在屏幕的位置
frame.setLocation(100,50);
// 窗体大小
frame.setSize(500,500);
// 显示窗体
frame.setVisible(true);
}
});
JButton button1 = new JButton("点");
button1.setSize(50, 25);
button1.setBackground(null);
button1.setBorderPainted(false);
button1.setLocation(50,80 );
button1.addActionListener(new ActionListener(){
//单击按钮执行的方法
public void actionPerformed(ActionEvent e) {
closeThis();
//创建新的窗口
JFrame frame1 = new JFrame("新窗口");
//设置在屏幕的位置
frame1.setLocation(100,50);
// 窗体大小
frame1.setSize(200,200);
JButton button2 = new JButton("点击我");
button2.setSize(50, 25);
button2.setBorderPainted(false);
button2.setLocation(50,80 );
button2.addActionListener(new ActionListener(){
//单击按钮执行的方法
public void actionPerformed(ActionEvent e) {
closeThis();
//创建新的窗口
JFrame frame = new JFrame("新窗口");
//设置在屏幕的位置
frame.setLocation(200,50);
// 窗体大小
frame.setSize(200,200);
// 显示窗体
frame.setVisible(true);
}
});
Panel pan = new Panel();
pan.setSize(100, 100);
frame1.add(button2);
frame1.add(pan);
// 显示窗体
frame1.setVisible(true);
}
});
fr.add(button);
fr.add(button1);
fr.setVisible(true);
}
public static void closeThis(){
fr.dispose();
}
}
算术我最棒 1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class compute extends JFrame {
public static void main(String[] args) {
new compute();
}
public compute() {
JFrame mainFrame = new JFrame("算数我最棒");
conputePanel jp1 = new conputePanel();
JPanel jp2 = new JPanel();
//退出按钮
JButton exitButton = new JButton("退出");
exitButton.addActionListener(new ActionListener()
{
// 事件源(注册事件)
@Override
public void actionPerformed(ActionEvent e)
{
mainFrame.dispose();
}
});
JPanel optionPanel = new JPanel();
ButtonGroup option = new ButtonGroup();
JRadioButton optionA = new JRadioButton("A.");
JRadioButton optionB = new JRadioButton("B.");
JRadioButton optionC = new JRadioButton("C.");
JRadioButton optionD = new JRadioButton("D.");
option.add(optionA);
option.add(optionB);
option.add(optionC);
option.add(optionD);
optionPanel.add(optionA);
optionPanel.add(optionB);
optionPanel.add(optionC);
optionPanel.add(optionD);
optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.Y_AXIS));
optionPanel.setBorder(BorderFactory.createTitledBorder("单选"));
jp2.add(exitButton);
mainFrame.add(jp1, BorderLayout.NORTH);
mainFrame.add(jp2, BorderLayout.SOUTH);
mainFrame.add(optionPanel, BorderLayout.CENTER);
mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
mainFrame.setLocation(500, 250);
mainFrame.setSize(500, 300);
mainFrame.setVisible(true);
}
}
//对计算panel随机计算式的设置
class conputePanel extends JPanel {
JLabel label;
int result;
//新建label
public conputePanel() {
super();
label = new JLabel();
add(label);
init();
}
//取1-100之间的随机值
public void init() {
int a;
int b;
String tm;
do {
a = (int) (Math.random() * 100) + 1;
b = (int) (Math.random() * 100) + 1;
} while (a == b || b == 1);
// 通过随机值随机取符号
switch ((int) (Math.random() * 4)) {
case 0:
tm = " + ";
label.setText("" + a + tm + b + " = ");
result = a + b;
break;
case 1:
tm = " - ";
label.setText("" + a + tm + b + " = ");
result = a - b;
break;
case 2:
tm = " × ";
label.setText("" + a + tm + b + " = ");
result = a * b;
break;
case 3:
tm = " ÷ ";
label.setText("" + a + tm + b + " = ");
result = a / b;
break;
// 当随机数不为0,1,2,3时的修正语句
default:
JOptionPane.showMessageDialog(null, "出错!");
System.exit(0);
}
// 当结果不为简单运算结果时,重新进行init()取值
if ((result < 0) || (result > 100) || ((a % b) != 0)) {
init();
}
return;
}
}
2
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class compute extends JFrame {
public static void main(String[] args) {
new compute();
}
public compute() {
JFrame mainFrame = new JFrame("算数我最棒");
conputePanel jp1 = new conputePanel();
JPanel buttonPanel = new JPanel();
//退出按钮
JButton exitButton = new JButton("退出");
exitButton.addActionListener(new ActionListener()
{
// 事件源(注册事件)
@Override
public void actionPerformed(ActionEvent e)
{
mainFrame.dispose();
}
});
buttonPanel.add(exitButton);
mainFrame.add(jp1, BorderLayout.NORTH);
mainFrame.add(buttonPanel, BorderLayout.SOUTH);
mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
mainFrame.setLocation(500, 250);
mainFrame.setSize(500, 300);
mainFrame.setVisible(true);
}
}
//对计算panel随机计算式的设置
class conputePanel extends JPanel {
JLabel label;
JRadioButton optionA,optionB,optionC,optionD;
int result;
//新建label
public conputePanel() {
super();
label = new JLabel();
add(label);
ButtonGroup option = new ButtonGroup();
optionA = new JRadioButton();
optionB = new JRadioButton();
optionC = new JRadioButton();
optionD = new JRadioButton();
option.add(optionA);
option.add(optionB);
option.add(optionC);
option.add(optionD);
add(optionA);
add(optionB);
add(optionC);
add(optionD);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBorder(BorderFactory.createTitledBorder("单选"));
init();
}
//取1-100之间的随机值
public void init() {
int a;
int b;
String tm;
do {
a = (int) (Math.random() * 100) + 1;
b = (int) (Math.random() * 100) + 1;
} while (a == b || b == 1);
// 通过随机值随机取符号
switch ((int) (Math.random() * 4)) {
case 0:
tm = " + ";
label.setText("" + a + tm + b + " = ");
result = a + b;
break;
case 1:
tm = " - ";
label.setText("" + a + tm + b + " = ");
result = a - b;
break;
case 2:
tm = " × ";
label.setText("" + a + tm + b + " = ");
result = a * b;
break;
case 3:
tm = " ÷ ";
label.setText("" + a + tm + b + " = ");
result = a / b;
break;
// 当随机数不为0,1,2,3时的修正语句
default:
JOptionPane.showMessageDialog(null, "出错!");
System.exit(0);
}
// 当结果不为简单运算结果时,重新进行init()取值
if ((result < 0) || (result > 100) || ((a % b) != 0)) {
init();
}
return;
}
}
3
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class compute extends JFrame {
public static void main(String[] args) {
new compute();
}
public compute() {
JFrame mainFrame = new JFrame("算数我最棒");
conputePanel jp1 = new conputePanel();
JPanel buttonPanel = new JPanel();
// 退出按钮
JButton exitButton = new JButton("退出");
exitButton.addActionListener(new ActionListener() {
// 事件源(注册事件)
@Override
public void actionPerformed(ActionEvent e) {
mainFrame.dispose();
}
});
buttonPanel.add(exitButton);
mainFrame.add(jp1, BorderLayout.NORTH);
mainFrame.add(buttonPanel, BorderLayout.SOUTH);
mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
mainFrame.setLocation(500, 250);
mainFrame.setSize(500, 300);
mainFrame.setVisible(true);
}
}
//对计算panel随机计算式的设置
class conputePanel extends JPanel {
JLabel label;
JRadioButton optionA, optionB, optionC, optionD;
int result;
int flag = -1;
// 新建label
public conputePanel() {
super();
label = new JLabel();
add(label);
ButtonGroup option = new ButtonGroup();
optionA = new JRadioButton();
optionB = new JRadioButton();
optionC = new JRadioButton();
optionD = new JRadioButton();
class MyItemListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
//强制转换为抽象按钮:AbstractButton是JButton、JCheckBox、JRadioButton的父类
//由于不知道谁会被选中,所以使用它们的父类
AbstractButton btn = (AbstractButton) e.getSource();//发生Event的源头
int state = e.getStateChange();
switch (state) {
case ItemEvent.SELECTED:
System.out.println(btn.getText() + "被选中");
}
}
}
optionA.addItemListener(new MyItemListener());
optionB.addItemListener(new MyItemListener());
optionC.addItemListener(new MyItemListener());
optionD.addItemListener(new MyItemListener());
option.add(optionA);
option.add(optionB);
option.add(optionC);
option.add(optionD);
add(optionA);
add(optionB);
add(optionC);
add(optionD);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBorder(BorderFactory.createTitledBorder("单选"));
init();
}
// 取1-100之间的随机值
public void init() {
int a, b, c0 = -1, c1 = -1, c2 = -1, c3 = -1;
String tm;
do {
a = (int) (Math.random() * 100) + 1;
b = (int) (Math.random() * 100) + 1;
} while (a == b || b == 1);
// 通过随机值随机取符号
switch ((int) (Math.random() * 4)) {
case 0:
tm = " + ";
label.setText("" + a + tm + b + " = ");
result = a + b;
optionA.setText(" A. " + result);
flag = 0;
break;
case 1:
tm = " - ";
label.setText("" + a + tm + b + " = ");
result = a - b;
optionB.setText(" B. " + result);
flag = 1;
break;
case 2:
tm = " × ";
label.setText("" + a + tm + b + " = ");
result = a * b;
optionC.setText(" C. " + result);
flag = 2;
break;
case 3:
tm = " ÷ ";
label.setText("" + a + tm + b + " = ");
result = a / b;
optionD.setText(" D. " + result);
flag = 3;
break;
// 当随机数不为0,1,2,3时的修正语句
default:
JOptionPane.showMessageDialog(null, "出错!");
System.exit(0);
}
// 设置错误单选项名称
if (flag != 0) {
do {
c0 = (int) (Math.random() * 10) + 1;
} while (c0 == result);
optionA.setText(" A. " + c0);
}
if (flag != 1) {
do {
c1 = (int) (Math.random() * 10) + 1;
} while (c1 == result || c1 == c0);
optionB.setText(" B. " + c1);
}
if (flag != 2) {
do {
c2 = (int) (Math.random() * 10) + 1;
} while (c2 == result || c2 == c0 || c2 == c1);
optionC.setText(" C. " + c2);
}
if (flag != 3) {
do {
c3 = (int) (Math.random() * 10) + 1;
} while (c3 == result || c3 == c0 || c3 == c1 || c3 == c2);
optionD.setText(" D. " + c3);
}
// 当结果不为简单运算结果时,重新进行init()取值
if ((result < 0) || (result > 100) || ((a % b) != 0)) {
init();
}
return;
}
}