Java图形化Swing示例
- 卡片布局
package org.dsg.jdk.graphics;
import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class SwingDemo {
public static void main(String[] args) {
Class<MyCardLayout> cardLayoutClass = MyCardLayout.class;
try {
Constructor<MyCardLayout> constructor = cardLayoutClass.getDeclaredConstructor();
constructor.newInstance();
} catch (NoSuchMethodException e) {
System.out.println("没有这种方法异常");
} catch (IllegalAccessException e) {
System.out.println("非法访问异常");
} catch (InstantiationException e) {
System.out.println("实例化异常");
} catch (InvocationTargetException e) {
System.out.println("调用目标异常");
}
}
private static class MyCardLayout extends JFrame {
public MyCardLayout() {
super("卡片布局");
JPanel panel = new JPanel();
CardLayout cardLayout = new CardLayout();
panel.setLayout(cardLayout);
JPanel blue = new JPanel();
JPanel red = new JPanel();
blue.setBackground(Color.blue);
red.setBackground(Color.red);
panel.add("blue", blue);
panel.add("red", red);
Button blueButton = new Button("blue");
Button redButton = new Button("red");
blueButton.addActionListener(event -> cardLayout.show(panel, "blue"));
redButton.addActionListener(event -> cardLayout.show(panel, "red"));
Panel buttonPanel = new Panel();
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
this.add(buttonPanel, BorderLayout.NORTH);
this.add(panel, BorderLayout.CENTER);
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
- 事件适配器示例,在窗口任意位置点击鼠标后,在控制台输出对应位置的坐标。
package org.dsg.jdk.graphics;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class SwingDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("事件适配器测试");
frame.setSize(500, 400);
MouseLs mouseLs = new MouseLs();
frame.addMouseListener(mouseLs);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static class MouseLs extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("鼠标点击了: (" + e.getX() + "," + e.getY() + ")");
}
}
}
- 画图示例,画了普通椭圆,蓝色带文字说明椭圆以及一个圆形。
package org.dsg.jdk.graphics;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class SwingDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("画图测试");
DrawJPanel component = new DrawJPanel();
frame.add(component);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static class DrawJPanel extends JPanel {
@Override
public void paint(Graphics g) {
List<String> fonts = getLocalFonts();
fonts.forEach(fontName -> {
if ("楷体".equals(fontName)) {
Font font = new Font(fontName, Font.ITALIC, 30);
g.setFont(font);
g.setColor(Color.red);
g.drawString("中文", 100, 100);
g.drawLine(100, 100, 200, 200);
g.drawOval(200, 200, 100, 100);
g.drawOval(300, 300, 200, 100);
g.setColor(Color.blue);
g.fillOval(100, 100, 200, 100);
}
});
}
}
private static List<String> getLocalFonts() {
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
List<String> fontList = new ArrayList<>();
Font[] fonts = environment.getAllFonts();
for (Font font : fonts) {
fontList.add(font.getName());
}
return fontList;
}
}
- 网格布局,两行三列的布局。
package org.dsg.jdk.graphics;
import javax.swing.*;
import java.awt.*;
public class SwingDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("网格布局管理器");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
GridLayout layout = new GridLayout(2, 3);
frame.setLayout(layout);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
- 对话框示例,首先是出现一个标题为“线程”的确认框,选择YES或NO,然后出现一个输入对话框,最后是一个标题为“点击测试”的选择对话框。
package org.dsg.jdk.graphics;
import javax.swing.*;
public class SwingDemo {
public static void main(String[] args) {
int i = JOptionPane.showConfirmDialog(null, "请选择", "线程", JOptionPane.YES_NO_OPTION);
System.out.println(i);
String input = JOptionPane.showInputDialog("明天放假如何");
System.out.println(input);
Object[] options = {"abc", "def", "hij"};
int m = JOptionPane.showOptionDialog(null, "点击测试", "线程", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[2]);
System.out.println(m);
}
}
- 事件监听示例,两个测试按钮,一个点击后控制台输出hello,另一个打印执行时间以及输出点不着。
package org.dsg.jdk.graphics;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
public class SwingDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Event Handler");
JPanel panel = new JPanel();
JLabel label = new JLabel("请点击");
JButton button = new JButton("Click");
JButton button2 = new JButton("Click2");
ClickAction action = new ClickAction();
Click2Action action2 = new Click2Action();
button.addActionListener(action);
button2.addActionListener(action2);
panel.add(label);
panel.add(button);
panel.add(button2);
frame.add(panel);
frame.setSize(400, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static class ClickAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("hello");
}
}
private static class Click2Action implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
long d = e.getWhen();
Date date = new Date(d);
System.out.println(date);
JButton button = (JButton) e.getSource();
button.setText("点不着");
String command = e.getActionCommand();
System.out.println(command);
}
}
}
- 鼠标事件示例,实现鼠标监听接口对应的方法。
package org.dsg.jdk.graphics;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class SwingDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("鼠标事件");
MouseL mouseL = new MouseL();
frame.addMouseListener(mouseL);
frame.setSize(200, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static class MouseL implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("点击发生了");
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("进入了窗口");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("鼠标离开了窗口");
}
}
}
- 无布局绝对定位
package org.dsg.jdk.graphics;
import javax.swing.*;
public class SwingDemo {
public static void main(String[] args) {
new LayoutWin();
}
private static class LayoutWin extends JFrame {
LayoutWin() {
super("绝对定位");
this.setLayout(null);
JButton button1 = new JButton("按钮1");
button1.setBounds(100, 100, 80, 50);
JButton button2 = new JButton("按钮2");
button2.setBounds(200, 100, 80, 50);
this.add(button1);
this.add(button2);
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}