2024年swing控件自动适配窗口大小

swing控件自动适配窗口大小展示一下 Swing 窗口的应用 IYTview java 一个接口 里面设置字体 行高 以及窗口待实现的方法 package com yt about gui view import java awt Color import java awt Font public interface IYTview Font

展示一下Swing窗口的应用

IYTview.java //一个接口,里面设置字体,行高,以及窗口待实现的方法

package com.yt.about_gui.view; import java.awt.Color; import java.awt.Font; public interface IYTview { Font topicFont = new Font("华文宋体", Font.BOLD, 30); int topicSize = topicFont.getSize(); int normlsize = 20; Color topicColor = new Color(3, 10, 232); Font normalFont = new Font("微软雅黑", Font.PLAIN, 15); int lineHeight = 25; int MARGIN = 15; int PADDING = 5; void init(); void reinit(); void dealAction(); void showView(); void exitView(); void redraw(); } 

 Main.java  // 主函数

package com.yt.about_gui.view; public class Main { public static void main(String[] args) { new Guiview().showView(); } }

Guiview.java  // 主界面

package com.yt.about_gui.view; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import javax.swing.ButtonGroup; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.border.TitledBorder; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class Guiview implements IYTview{ private static final int viewWidth = 800; private static final int viewheight = 600; private static final double proportion = 0.68; private JFrame jFramemain; private Container container; private JLabel jLabelTopic; private JPanel jPanelEditer; private JPanel jPanelList; private JPanel jPanelButton; private JLabel jLabelId; private JTextField jTextFieldId; private JButton jbtnAddId; private JLabel jLabelName; private JTextField jTextFieldName; private JLabel jLabelPassword; private JPasswordField jpswField; private JLabel jLabelSex; private JRadioButton jButtonman; private JRadioButton jButtonWoman; private JLabel jLabelBirthday; private JComboBox<String> jComboBoxDay; private JLabel jLabelDay; private JComboBox<String> jComboBoxMonth; private JLabel jLabelMonth; private JComboBox<String> jComboBoxYear; private JLabel jLabelYear; private JPanel jPanelHobby; private JButton jbtnHobbyAll; private JButton jbtnRehobby; private JButton jbtnopposite; private JButton jbtnAdd; private JButton jbtnRemove; private JButton jbtnModif; private JPanel jPanelSethobby; private List<HobbyModel> hobbies; private JCheckBox[] jckbHobbies; private JTextArea jtaIntroduce; private JScrollPane jscpIntroduce; private JScrollPane jscpStuList; private DefaultListModel<OneStuInfo> dflmStuList; private JList<OneStuInfo> stuList; public Guiview() { hobbies = new ArrayList<>(); hobbies.add(new HobbyModel("01","睡觉")); hobbies.add(new HobbyModel("02","吃饭")); hobbies.add(new HobbyModel("03","游戏")); hobbies.add(new HobbyModel("04","美女")); hobbies.add(new HobbyModel("05","泡妞")); hobbies.add(new HobbyModel("06","追剧")); hobbies.add(new HobbyModel("07","打球")); hobbies.add(new HobbyModel("08","lol")); hobbies.add(new HobbyModel("09","联盟")); hobbies.add(new HobbyModel("10","编程")); hobbies.add(new HobbyModel("11","瞌睡")); hobbies.add(new HobbyModel("12","鞋子")); jckbHobbies = new JCheckBox[hobbies.size()]; init(); reinit(); dealAction(); } @Override public void init() { jFramemain = new JFrame("学生信息管理系统"); // 创建一个窗口,名字为学生信息管理 jFramemain.setSize(viewWidth, viewheight); // 设置大小 jFramemain.setLocationRelativeTo(null); // 设置窗口相对于指定组件的位置,将窗口置于屏幕中间 jFramemain.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//设置点击关闭窗口的动作,这里是do_Nothing,什么也不做! container = jFramemain.getContentPane(); container.setLayout(null); jLabelTopic = new JLabel("学生信息管理", JLabel.CENTER); jLabelTopic.setSize(0, topicSize); jLabelTopic.setForeground(topicColor); jLabelTopic.setLocation(0, 0); jLabelTopic.setBackground(Color.pink); jLabelTopic.setOpaque(true); jLabelTopic.setFont(topicFont); container.add(jLabelTopic); int top = jLabelTopic.getHeight(); int jPanelButtonwidth = 100; int Editerwidth = (int)((viewWidth - 2*PADDING) * proportion); int Editerheight = viewheight - jLabelTopic.getWidth() - jPanelButtonwidth -3* PADDING; jPanelEditer = new JPanel(); jPanelEditer.setSize(Editerwidth, Editerheight); jPanelEditer.setLocation(0, top); jPanelEditer.setLayout(null); //jPanelEditer.setBackground(Color.DARK_GRAY); container.add(jPanelEditer); jPanelList = new JPanel(); jPanelList.setSize(viewWidth - Editerwidth, Editerheight); jPanelList.setLocation(jPanelEditer.getX() + jPanelEditer.getWidth(), top); container.add(jPanelList); top = PADDING; jLabelId = new JLabel("学 号", JLabel.LEFT); jLabelId.setFont(normalFont); jLabelId.setBounds(PADDING, top, 3*lineHeight, lineHeight); jPanelEditer.add(jLabelId); jTextFieldId = new JTextField(); jTextFieldId.setEnabled(false); jTextFieldId.setForeground(Color.RED); jTextFieldId.setFont(normalFont); jTextFieldId.setBounds(jLabelId.getX() + jLabelId.getWidth(), top, 10*lineHeight, lineHeight); jPanelEditer.add(jTextFieldId); jbtnAddId = new JButton("生成"); jbtnAddId.setFont(normalFont); jbtnAddId.setBounds(jTextFieldId.getX() + jTextFieldId.getWidth(), top, 3*lineHeight, lineHeight); jPanelEditer.add(jbtnAddId); top += lineHeight + PADDING; jLabelName = new JLabel("姓 名", JLabel.LEFT); jLabelName.setFont(normalFont); jLabelName.setBounds(PADDING, top, 3*lineHeight, lineHeight); jPanelEditer.add(jLabelName); jTextFieldName = new JTextField(); jTextFieldName.setFont(normalFont); jTextFieldName.setBounds(jLabelName.getX() + jLabelName.getWidth(), top, 13*lineHeight, lineHeight); jPanelEditer.add(jTextFieldName); top += lineHeight + PADDING; jLabelPassword = new JLabel("密 码", JLabel.LEFT); jLabelPassword.setFont(normalFont); jLabelPassword.setBounds(PADDING, top, 3*lineHeight, lineHeight); jPanelEditer.add(jLabelPassword); jpswField = new JPasswordField(); jpswField.setFont(normalFont); jpswField.setBounds(jLabelPassword.getX() + jLabelPassword.getWidth(), top, 13*lineHeight, lineHeight); jPanelEditer.add(jpswField); top += lineHeight + PADDING; jLabelSex = new JLabel("性 别", JLabel.LEFT); jLabelSex.setFont(normalFont); jLabelSex.setBounds(PADDING, top, 3*lineHeight, lineHeight); jPanelEditer.add(jLabelSex); ButtonGroup bg = new ButtonGroup(); jButtonman = new JRadioButton("男"); bg.add(jButtonman); jButtonman.setFont(normalFont); jButtonman.setBounds(jLabelSex.getX() + jLabelSex.getWidth() + lineHeight, top, 3*lineHeight, lineHeight); jPanelEditer.add(jButtonman); jButtonWoman = new JRadioButton("女"); bg.add(jButtonWoman); jButtonWoman.setFont(normalFont); jButtonWoman.setBounds(jButtonman.getX() + jButtonman.getWidth() + lineHeight, top, 3*lineHeight, lineHeight); jPanelEditer.add(jButtonWoman); top += lineHeight + PADDING; jLabelBirthday = new JLabel("出生日期", JLabel.LEFT); jLabelBirthday.setFont(normalFont); jLabelBirthday.setBounds(PADDING, top, 3*lineHeight, lineHeight); jPanelEditer.add(jLabelBirthday); jComboBoxYear = new JComboBox<>(); jComboBoxYear.setBounds(jLabelBirthday.getX() + 3*lineHeight, top, 3*lineHeight, lineHeight); jComboBoxYear.setFont(normalFont); jPanelEditer.add(jComboBoxYear); jLabelYear = new JLabel("年"); jLabelYear.setFont(normalFont); jLabelYear.setBounds(jComboBoxYear.getX() + jComboBoxYear.getWidth(), top, lineHeight, lineHeight); jPanelEditer.add(jLabelYear); jComboBoxMonth = new JComboBox<>(); jComboBoxMonth.setBounds(jComboBoxYear.getX() + jComboBoxYear.getWidth() + 3*PADDING, top, 3*lineHeight-PADDING, lineHeight); jComboBoxMonth.setFont(normalFont); jPanelEditer.add(jComboBoxMonth); jLabelMonth = new JLabel("月"); jLabelMonth.setFont(normalFont); jLabelMonth.setBounds(jComboBoxMonth.getX() + jComboBoxMonth.getWidth(), top, lineHeight, lineHeight); jPanelEditer.add(jLabelMonth); jComboBoxDay = new JComboBox<>(); jComboBoxDay.setBounds(jComboBoxMonth.getX() + jComboBoxMonth.getWidth() + 3*PADDING, top, 3*lineHeight-PADDING, lineHeight); jComboBoxDay.setFont(normalFont); jPanelEditer.add(jComboBoxDay); jLabelDay = new JLabel("日"); jLabelDay.setFont(normalFont); jLabelDay.setBounds(jComboBoxDay.getX() + jComboBoxDay.getWidth(), top, lineHeight, lineHeight); jPanelEditer.add(jLabelDay); top += lineHeight + PADDING; jPanelHobby = new JPanel(); jPanelHobby.setBounds(PADDING, top, Editerwidth - PADDING, 6*lineHeight); jPanelEditer.add(jPanelHobby); TitledBorder titledBorder = new TitledBorder("爱好"); titledBorder.setTitleColor(Color.PINK); jPanelHobby.setBorder(titledBorder); jPanelHobby.setLayout(new FlowLayout()); for(int i = 0; i < jckbHobbies.length; i++){ jckbHobbies[i] = new JCheckBox(hobbies.get(i).toString()); jckbHobbies[i].setName((i + 100 + "").substring(1)); jckbHobbies[i].setFont(normalFont); jckbHobbies[i].setPreferredSize(new Dimension(4*lineHeight, lineHeight)); jPanelHobby.add(jckbHobbies[i]); } top += jPanelHobby.getHeight(); jPanelSethobby = new JPanel(); jPanelSethobby.setLayout(new FlowLayout()); jPanelSethobby.setBounds(PADDING, top, Editerwidth, lineHeight + 2*PADDING); jbtnHobbyAll = new JButton("全选"); jbtnHobbyAll.setFont(normalFont); jbtnHobbyAll.setSize(2*lineHeight, lineHeight); jPanelSethobby.add(jbtnHobbyAll); jbtnRehobby = new JButton("重置"); jbtnRehobby.setFont(normalFont); jbtnRehobby.setSize(2*lineHeight, lineHeight); jPanelSethobby.add(jbtnRehobby); jbtnopposite = new JButton("反选"); jbtnopposite.setFont(normalFont); jbtnopposite.setSize(2*lineHeight, lineHeight); jPanelSethobby.add(jbtnopposite); jPanelEditer.add(jPanelSethobby); top += jPanelSethobby.getHeight(); jtaIntroduce = new JTextArea(); jscpIntroduce = new JScrollPane(jtaIntroduce); TitledBorder titIntroduce = new TitledBorder("介绍"); //titIntroduce.setTitlePosition(jscpIntroduce.HEIGHT); titIntroduce.setTitleColor(Color.BLUE); //titIntroduce.setTitleJustification(); jscpIntroduce.setBorder(titIntroduce); jscpIntroduce.setBounds(PADDING, top, Editerwidth - PADDING, 5*lineHeight + 4*PADDING); jPanelEditer.add(jscpIntroduce); top = jscpIntroduce.getHeight() + jscpIntroduce.getY() + jLabelTopic.getHeight() + PADDING; jPanelButton = new JPanel(); jPanelButton.setLayout(new BorderLayout()); jPanelButton.setBounds(PADDING, top, 8*lineHeight, lineHeight + 2*PADDING); container.add(jPanelButton); jbtnAdd = new JButton("添加"); jbtnAdd.setEnabled(false); jbtnAdd.setFont(normalFont); jbtnAdd.setPreferredSize(new Dimension(4*lineHeight, lineHeight)); jPanelButton.add(jbtnAdd, BorderLayout.WEST); jbtnModif= new JButton("修改"); jbtnModif.setEnabled(false); jbtnModif.setFont(normalFont); jbtnModif.setPreferredSize(new Dimension(4*lineHeight, lineHeight)); jPanelButton.add(jbtnModif,BorderLayout.EAST); jbtnRemove = new JButton("退出"); jbtnRemove.setFont(normalFont); jbtnRemove.setSize(4*lineHeight, lineHeight + 2*PADDING); jbtnRemove.setLocation(Editerwidth + 6*lineHeight, top); container.add(jbtnRemove); dflmStuList = new DefaultListModel<>();//创建一个列表数据模型 stuList = new JList<>(dflmStuList);//创建一个列表框里面数据类型是dflmStuList stuList.setFont(normalFont); //设置字体 jscpStuList = new JScrollPane(stuList); //将列表框放在JScrollPane面板里 jscpStuList.setPreferredSize(new Dimension(240,450));//设置面板大小 TitledBorder listBorder = new TitledBorder("用户列表");//设置面板边框和标题 listBorder.setTitleColor(Color.BLUE);//设置标题颜色 jscpStuList.setBorder(listBorder);//设置边框 jPanelList.add(jscpStuList);//添加到面板里 } private String getId() { SimpleDateFormat df = new SimpleDateFormat("yyMM"); String stuId = df.format(new java.util.Date()); int count = dflmStuList.getSize() + 1; return stuId + (count + 1000 + "").substring(1); } private void shownull() { jTextFieldName.setText(""); jpswField.setText(""); jtaIntroduce.setText(""); jTextFieldId.setText(""); for(int i = 0; i < jckbHobbies.length; i++) { jckbHobbies[i].setSelected(false); } jButtonman.setSelected(false); jButtonWoman.setSelected(false); jComboBoxYear.setSelectedItem("2000"); jComboBoxMonth.setSelectedItem("1"); jComboBoxDay.setSelectedItem("1"); jtaIntroduce.setText(""); } private void changeview(boolean change) { jTextFieldName.setEnabled(change); jpswField.setEnabled(change); jtaIntroduce.setEnabled(change); for(int i = 0; i < jckbHobbies.length; i++) { jckbHobbies[i].setEnabled(change); } jButtonman.setEnabled(change); jButtonWoman.setEnabled(change); jComboBoxYear.setEnabled(change); jComboBoxMonth.setEnabled(change); jComboBoxDay.setEnabled(change); jtaIntroduce.setEnabled(change); jbtnHobbyAll.setEnabled(change); jbtnRehobby.setEnabled(change); jbtnopposite.setEnabled(change); } private void showList(){ OneStuInfo oneStuInfo = new OneStuInfo(); oneStuInfo.setId(getId()); oneStuInfo.setName(jTextFieldName.getText()); oneStuInfo.setPassword(jpswField.getUIClassID()); oneStuInfo.setSex(!jButtonman.isSelected()); oneStuInfo.setIntroduce(jtaIntroduce.getText()); oneStuInfo.setBirthday((String)jComboBoxYear.getSelectedItem() + "-" + jComboBoxMonth.getSelectedItem() + "-" + jComboBoxDay.getSelectedItem()); HobbyModel[] hobbies = new HobbyModel[jckbHobbies.length]; for(int i = 0; i < jckbHobbies.length; i++) { hobbies[i] = new HobbyModel(); if(jckbHobbies[i].isSelected()) { hobbies[i].setIsselect(true); } else { hobbies[i].setIsselect(false); } } oneStuInfo.setHobbies(hobbies); dflmStuList.addElement(oneStuInfo); } private void showStuInfo(OneStuInfo oneStuInfo) { jTextFieldName.setText(oneStuInfo.getName()); jpswField.setText(oneStuInfo.getPassword()); jtaIntroduce.setText(oneStuInfo.getIntroduce()); jTextFieldId.setText(oneStuInfo.getId()); HobbyModel[] hobbies = oneStuInfo.getHobbies(); for(int i = 0; i < hobbies.length; i++) { if(hobbies[i].isIsselect()) { jckbHobbies[i].setSelected(true); } else { jckbHobbies[i].setSelected(false); } } jButtonman.setSelected(!oneStuInfo.getSex()); jButtonWoman.setSelected(oneStuInfo.getSex()); String birthday = oneStuInfo.getBirthday(); String[] buffer = birthday.split("-"); jComboBoxYear.setSelectedItem(buffer[0]); jComboBoxMonth.setSelectedItem(buffer[1]); jComboBoxDay.setSelectedItem(buffer[2]); } private void setmaxDay(){ int year = (Integer.valueOf((String)jComboBoxYear.getSelectedItem())) - 1; int month = (Integer.valueOf((String)jComboBoxMonth.getSelectedItem())) - 1; Calendar day = Calendar.getInstance(); day.set(year, month, 1); jComboBoxDay.removeAllItems(); for(int i = 1; i <= day.getActualMaximum(Calendar.DAY_OF_MONTH); i++) { jComboBoxDay.addItem(i + ""); } } @Override public void reinit() { Calendar today = Calendar.getInstance(); int minyear = 130; int maxyear = 10; int year = today.get(Calendar.YEAR); for(int i = year - minyear; i < (year - maxyear); i++) { jComboBoxYear.addItem(i + ""); } jComboBoxYear.setSelectedItem("2000"); for(int i = 0; i < 12; i++) { jComboBoxMonth.addItem(i + 1 + ""); } jComboBoxMonth.setSelectedItem("1"); } @Override public void dealAction() { jFramemain.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { exitView(); } }); jbtnRemove.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { exitView(); } }); jbtnHobbyAll.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for(int i = 0; i < jckbHobbies.length; i++){ jckbHobbies[i].setSelected(true); } } }); jbtnRehobby.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for(int i = 0; i < jckbHobbies.length; i++){ jckbHobbies[i].setSelected(false); } } }); jbtnopposite.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for(int i = 0; i < jckbHobbies.length; i++){ jckbHobbies[i].setSelected(!jckbHobbies[i].isSelected()); } } }); jbtnAddId.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jbtnAdd.setEnabled(true); jbtnAddId.setEnabled(false); jbtnModif.setEnabled(false); changeview(true); jTextFieldId.setText(getId()); } }); jbtnAdd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jbtnAdd.setEnabled(false); jbtnAddId.setEnabled(true); showList(); shownull(); } }); jComboBoxYear.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED){ setmaxDay(); } } }); jComboBoxMonth.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED){ setmaxDay(); } } }); stuList.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { if(e.getValueIsAdjusting()) { OneStuInfo oneStuInfo = stuList.getSelectedValue(); showStuInfo(oneStuInfo); changeview(false); jbtnAddId.setEnabled(true); jbtnModif.setEnabled(true); } } }); jbtnModif.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(jbtnModif.getText().equals("修改")){ changeview(true); jbtnAddId.setEnabled(false); jbtnModif.setText("确定"); } else { jbtnModif.setText("修改"); jbtnAddId.setEnabled(true); changeview(false); //TODO } } }); } @Override public void showView() { jFramemain.setVisible(true); redraw(); } @Override public void exitView() { int isclose = JOptionPane.showConfirmDialog(jFramemain, "确认是否退出", "杨涛的窗口", JOptionPane.YES_NO_OPTION); if(isclose == JOptionPane.YES_OPTION){ jFramemain.dispose(); } } @Override public void redraw() { jLabelTopic.setSize(container.getWidth(), jLabelTopic.getHeight()); jPanelList.setSize(jLabelTopic.getWidth() - jPanelEditer.getWidth() ,jPanelEditer.getHeight()); } }

HobbyModel.java // 爱好模板

package com.yt.about_gui.view; public class HobbyModel { private String hobby; private String id; private boolean isselect; public HobbyModel() { // TODO Auto-generated constructor stub } public HobbyModel(String id, String hobby) { this.id = id; this.hobby = hobby; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } public String getId() { return id; } public void setId(String id) { this.id = id; } @Override public String toString() { return hobby; } public boolean isIsselect() { return isselect; } public void setIsselect(boolean isselect) { this.isselect = isselect; } }

OneStuInfo.java  // 学生信息模板

package com.yt.about_gui.view; public class OneStuInfo { private String id; private String name; private String password; private boolean sex; private String birthday; private HobbyModel[] hobbies; private String introduce; public OneStuInfo() { // TODO Auto-generated constructor stub } public OneStuInfo(String id, String name, String password, boolean sex, String birthday, HobbyModel[] hobbies, String introduce) { this.id = id; this.name = name; this.password = password; this.sex = sex; this.birthday = birthday; this.hobbies = hobbies; this.introduce = introduce; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean getSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public HobbyModel[] getHobbies() { return hobbies; } public void setHobbies(HobbyModel[] hobbies) { this.hobbies = hobbies; } public String getIntroduce() { return introduce; } public void setIntroduce(String introduce) { this.introduce = introduce; } @Override public String toString() { return id + " " + (sex == false ? "男" :"女") + " " + name; } }

详细讲解点击以下网址

https://blog.csdn.net/_/article/details/

 

知秋君
上一篇 2024-11-10 21:48
下一篇 2024-11-09 09:36

相关推荐