[JavaSwing练习] 第一天:JFrame,JPanel,JTabbedPane图集
有一个不错的点子(制作周期大概半个月), 这次完全想靠自己肝出来, 首先得会一种GUI, 思来想去, 那就用JavaSwing吧, 顺便把这学期的Java课设作为作品的一个小分支(日常水课设), 先熟悉几天之后会用到的JavaSwing容器/组件吧...
JFrame
myWindow.java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class myWindow extends JFrame{
JPanel panel = new JPanel();
JButton btn = new JButton("To New World!→");
myWindow(){
this.setTitle("My Window");
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
panel.add(btn);
btn.setFont(new Font(null, Font.PLAIN, 30));
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showNewWindow();
}
});
this.setContentPane(panel);
this.setVisible(true);
}
public void showNewWindow(){
JFrame newJFrame = new JFrame("new window");//名字
newJFrame.setFont(new Font(null, Font.PLAIN, 30));//字体
newJFrame.setSize(250, 250);//大小
newJFrame.setResizable(false);//大小不可变
newJFrame.setLocationRelativeTo(this);//位置
newJFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//只关闭新窗口
JPanel panel = new JPanel(new GridLayout(1,1));
JLabel label = new JLabel("你所处的是新世界");
label.setFont(new Font(null, Font.PLAIN, 30));
// label.setHorizontalAlignment(SwingConstants.CENTER);
// label.setVerticalAlignment(SwingConstants.CENTER);
panel.add(label);
newJFrame.setContentPane(panel);
newJFrame.setVisible(true);
}
}
JPanel
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame jf = new JFrame("用户登录");
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//使用默认的浮动布局
JPanel panel01 = new JPanel();
panel01.add(new JLabel("用户名"));
panel01.add(new JTextField(10));
JPanel panel02 = new JPanel();
panel02.add(new JLabel("密 码"));
panel02.add(new JPasswordField(10));
//使用浮动布局, 且容器中组件居中显示
JPanel panel03 = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel03.add(new JButton("登录"));
panel03.add(new JButton("注册"));
// 创建一个垂直盒子容器
Box vBox = Box.createVerticalBox();
vBox.add(panel01);
vBox.add(panel02);
vBox.add(panel03);
jf.setContentPane(vBox);
jf.setLocationRelativeTo(null);
jf.setSize(300, 200);
jf.setVisible(true);
}
}
JTabbedPane
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame jf = new JFrame("photo");
jf.setSize(300, 300);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
// 创建选项卡面板
final JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setFont(new Font(null, Font.PLAIN, 30));
tabbedPane.addTab("Tab01", createTextPanel(1));
tabbedPane.addTab("Tab02", createTextPanel(2));
tabbedPane.addTab("Tab03", createTextPanel(3));
tabbedPane.addTab("Tab04", createTextPanel(4));
tabbedPane.addTab("Tab05", createTextPanel(5));
tabbedPane.addTab("Tab06", createTextPanel(6));
tabbedPane.addTab("Tab07", createTextPanel(7));
tabbedPane.addTab("Tab08", createTextPanel(8));
tabbedPane.addTab("Tab09", createTextPanel(9));
// 添加选项卡选中状态改变的监听器
tabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("当前选中的是: " + tabbedPane.getSelectedIndex());
}
});
// 设置默认选中的选项卡
tabbedPane.setSelectedIndex(5);
jf.setContentPane(tabbedPane);
jf.setVisible(true);
}
// 创建一个面板,面板中心显示一个标签表示选项卡需要显示的内容
private static JComponent createTextPanel(int num) {
// 1 * 1 的网格布局
JPanel panel = new JPanel(new GridLayout(1, 1));
JLabel label = new JLabel();
label.setIcon(new ImageIcon(num+".jpg"));
label.setFont(new Font(null, Font.PLAIN, 50));
label.setHorizontalAlignment(SwingConstants.CENTER);
// 添加标签到面板
panel.add(label);
return panel;
}
}
本文来自博客园,作者:泥烟,CSDN同名, 转载请注明原文链接:https://www.cnblogs.com/Knight02/p/15799047.html