对于硬编码的GUI来说,其实没必要研究那么多,对于绝大多数人是没有什么研究价值的,但是研究研究也是蛮好玩的。今天我们一起来解决这个问题。
这个问题在度娘里面还是很普遍的 。
下面虽然有热心网友给出了建议,但是貌似不是很具体,也不是很易懂(可能是我太菜= =)。!
这个答案看似可行,其实不是这样的,我来解释下:
对于一个JFrame中,JFrame其实是一个框架,里面什么也没有,你可以理解它就是给你一个窗口,也可以理解它是一个Swing当中一个类似于main方法的东西,因为所有的组件和容器都要在这上面才能显示,就像所有的成员方法都要在main中才能得到具体的表现(这句话不正确,不要较真)。这位解答的用户,Ta的意思是,在一个JFrame中同时存在两个Jpanel,一个设置为set可见,一个设置为set不可见,那么当你需要哪一个的时候,你通过按钮的监听事件,将它们的需要显示出来的设置为可见,不需要显示的设置为不可见。看上去很完美,也确实有一定可行性,但是不知道这位网友JFrame设置的Layout是怎样的,当使用绝对定位(layout为空),如果想实现“看上去的”切换窗口,必要在同一位置,但是同一位置设置两个Jpanel是否可行呢,我建议大家去试一下,我实验了,是不可行的,如果用C中的指针去看这件事情就很好理解了:当我们为JFrame中某个位置x设置了一个Jpanel A时,实际上是x位置的指针指向了这个A的地址,此时我们再将JFrame中的位置x上增加一个Jpanel B时,实际上是将x位置的执政指向了B的地址,那么,实际上JFrame中的x位置只能指向一个Jpanel。所以即使你设置了A可见,B可见,只会是什么都不显示,而不是显示A!!!其实如果能找到一种Layout可以自动填充空白部分就行了,我不知道有没有,有的话可以一起学下!
接下来就是我的解决方案了!
package LibrarySyste;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
//上面的不用看public class View extends JFrame {
private JPanel contentPane;
JPanel panel;//主函数提供显示,不用看!
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
View frame = new View();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
//先看一下,我下面具体解释
public View() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 725, 624);
//新建一个Jpanel,setContentPane这个不懂我下面解释一下,听不懂我说的,建议去看百度
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//亮点在这,panel一定要是全局的!
MainView m = new MainView();
panel = m.view();
panel.setBounds(0, 49, 693, 487);
contentPane.add(panel);
panel.setLayout(null);
//菜单
JMenuBar menuBar = new JMenuBar();
menuBar.setBounds(0, 0, 693, 50);
contentPane.add(menuBar);
JMenu mnNewMenu = new JMenu("菜单");
menuBar.add(mnNewMenu);
JMenuItem menuItem = new JMenuItem("修改信息");
mnNewMenu.add(menuItem);
JMenuItem menuItem_1 = new JMenuItem("查询信息");
mnNewMenu.add(menuItem_1);
JMenuItem menuItem_2 = new JMenuItem("删除信息");
mnNewMenu.add(menuItem_2);
JMenuItem menuItem_3 = new JMenuItem("增加图书");
mnNewMenu.add(menuItem_3);//查找图书的按钮监听器
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
contentPane.remove(panel);
SelectView s = new SelectView();
panel = s.view();
panel.setBounds(0, 49, 693, 487);
contentPane.add(panel);
panel.setLayout(null);
update(getGraphics());
}
});
//增加图书的按钮监听器
menuItem_3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
contentPane.remove(panel);
MainView m = new MainView();
panel= m.view();
panel.setBounds(0, 49, 693, 487);
contentPane.add(panel);
panel.setLayout(null);
update(getGraphics());
System.out.println("asdasdasdsa");
}
});
}
}
以上代码中涉及到两个类我没有具体给出MainView()和SelectView(),这两个类呢,是创建各自的Jpanel(即两个不同的窗口、界面),然后通过view返回出来。
声明一下:本人使用绝对定位不是我硬编码技术好,是因为我使用了Swing Design插件,拖动式的创建GUI,很快,不好就是不可以根据窗口大小的改变而改变组件的大小,不过呢,我个人认为无所谓,因为Java的出现,就是为了提高软件开发人员的生产效率,你们说直接拖动是不是比自己写,自己画来的快的多!?想下载的,自己去问度娘!
setContentPane的意思和add差不多,只不过是setContentPane是完全替代了JFrame。
为什么是用全局的Jpanel panel ,是因为如果是新建的Jpanel,那么在哪建呢?在监听器里面建还是在外面建?进入监听器后,要执行remove操作(等下再解释为什么要remove),如果不使用全局的Jpanel,那么第一次remove谁?第二次呢?第三次呢?remove的原因是,在ContentPane中,我们加入了Jpanel A,那么A中所有的组件都加进去了,那么A中每个组件都有一个位置,如果不完全移除,那么某些位置上的组件就会遗留(解释有点牵强哈,但是你不移除就是行!不服你试试!)。
使用全局的Jpanel后,我让ContentPane中只存在一个Jpanel panel,那么不论我在任何界面,我移除panel就等于移除了整个界面!而Jmenu不在panel中,这也是非常重要的,因为如果Jmenu在panel中,那么它的监听器就会非常麻烦,比如说,我进入了MainView,MainView中也存在一个Jmenu,那么这样就形成了监听事件的“递归”,看似是切换了窗口,实际上是新建了一个新的窗口!
好了,这就是我“对于JFrame通过菜单栏切换窗口、界面”的解决方案,如果有错误,欢迎指出,如果有疑问,可以留言。
下面附上
MainView()和SelectView()的代码:
MainView():
package LibrarySyste;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
public class MainView extends JFrame {
private JPanel contentPane;
private JPanel contentPane2;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JPanel contentPane_x;
public MainView() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 709, 626);
contentPane = new JPanel();
contentPane.setForeground(Color.WHITE);
contentPane.setBackground(new Color(0, 255, 255));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
add(contentPane);
contentPane.setLayout(null);
//这里的时编码问题,实际上按钮上的文字提示
JLabel label = new JLabel("\u56FE\u4E66\u540D\uFF1A");
label.setBounds(26, 86, 120, 35);
contentPane.add(label);
JLabel label_1 = new JLabel("\u4F5C \u8005\uFF1A");
label_1.setBounds(26, 147, 120, 35);
contentPane.add(label_1);
JLabel label_2 = new JLabel("\u4EF7 \u683C\uFF1A");
label_2.setBounds(26, 206, 120, 35);
contentPane.add(label_2);
JLabel label_3 = new JLabel("\u5206 \u7C7B\uFF1A");
label_3.setBounds(26, 267, 120, 35);
contentPane.add(label_3);
textField = new JTextField();
textField.setBounds(149, 83, 156, 41);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(149, 144, 156, 41);
contentPane.add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(149, 203, 156, 41);
contentPane.add(textField_2);
JComboBox comboBox = new JComboBox();
comboBox.setForeground(new Color(0, 0, 0));
comboBox.setBackground(new Color(255, 255, 255));
comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u9009\u62E9\u5206\u7C7B", "\u6587 \u5B66", "\u897F \u6587", "\u79D1 \u5B66", "\u7406 \u5DE5", "\u6742 \u5FD7"}));
comboBox.setToolTipText("");
comboBox.setBounds(149, 264, 156, 41);
contentPane.add(comboBox);
JButton button = new JButton("\u52A0\u5165\u4E66\u5E93");
button.setForeground(Color.WHITE);
button.setBackground(Color.DARK_GRAY);
button.setBounds(385, 346, 165, 71);
contentPane.add(button);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBackground(new Color(0, 128, 128));
lblNewLabel.setForeground(Color.WHITE);
//这个图片是我本机的,你们显示不了
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Administrator\\Desktop\\77c6a7efce1b9d167213535af0deb48f8c546431.jpg"));
lblNewLabel.setBounds(348, 41, 260, 279);
contentPane.add(lblNewLabel);
}
public JPanel view() {
return contentPane;
}
}
SelectView():
package LibrarySyste;
import java.awt.BorderLayout;
import LibrarySyste.*;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.ListSelectionModel;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;
public class SelectView extends JFrame{
private JPanel contentPane_x;
private static JPanel contentPane_x_copy;
private JTextField textField_x_1;
private JTextField textField_x_2;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SelectView frame = new SelectView();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SelectView() {
setBounds(100, 100, 709, 626);
contentPane_x = new JPanel();
contentPane_x.setForeground(Color.WHITE);
contentPane_x.setBackground(new Color(0, 255, 255));
contentPane_x.setBorder(new EmptyBorder(5, 5, 5, 5));
add(contentPane_x);
contentPane_x.setLayout(null);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBackground(new Color(0, 128, 128));
lblNewLabel.setForeground(Color.WHITE);
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Administrator\\Desktop\\77c6a7efce1b9d167213535af0deb48f8c546431.jpg"));
lblNewLabel.setBounds(348, 41, 260, 279);
contentPane_x.add(lblNewLabel);
textField_x_1 = new JTextField();
textField_x_1.setBounds(119, 92, 156, 41);
contentPane_x.add(textField_x_1);
textField_x_1.setColumns(10);
textField_x_2 = new JTextField();
textField_x_2.setColumns(10);
textField_x_2.setBounds(119, 156, 156, 41);
contentPane_x.add(textField_x_2);
JLabel label_x_1 = new JLabel("\u4E66\u540D\uFF1A");
label_x_1.setBounds(26, 98, 90, 35);
contentPane_x.add(label_x_1);
JLabel label_x_2 = new JLabel("\u4F5C\u8005\uFF1A");
label_x_2.setBounds(26, 159, 90, 35);
contentPane_x.add(label_x_2);
JButton button = new JButton(" \u67E5 \u627E");
button.setHorizontalAlignment(SwingConstants.LEFT);
button.setFont(new Font("微软雅黑", Font.PLAIN, 18));
button.setBounds(185, 235, 90, 46);
contentPane_x.add(button);
JScrollPane gun = new JScrollPane();
gun.setBounds(26, 331, 502, 181);
contentPane_x.add(gun);
table = new JTable();
table.setEnabled(false);
table.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
},
new String[] {
"\u7F16\u53F7", "\u4E66\u540D", "\u4F5C\u8005", "\u4EF7\u683C", "\u7C7B\u578B"
}
));
table.getColumnModel().getColumn(1).setPreferredWidth(102);
table.getColumnModel().getColumn(2).setPreferredWidth(79);
table.getColumnModel().getColumn(3).setPreferredWidth(61);
table.getColumnModel().getColumn(4).setPreferredWidth(70);
gun.setViewportView(table);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ResultSet rs = SYSTEM.result("select * from books where name = '"+textField_x_1.getText()+"'");
try {
TableModel model = table.getModel();
int row = 0;
while(rs.next())
{
model.setValueAt(rs.getInt(1), row, 0);
model.setValueAt(rs.getString(2), row, 1);
model.setValueAt(rs.getString(3), row, 2);
model.setValueAt(rs.getString(4), row, 3);
model.setValueAt(rs.getString(5), row, 4);
row++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public JPanel view() {
return contentPane_x;
}
}
附上代码运行图片:
切换后
功能你们没法实现,因为我的业务逻辑不在这上面,给你们也用不了,因为使用JDBC编程。有兴趣的可以找我!
希望可以帮助到你们,喜欢的可以点赞,不喜欢的。。。。就算了嘛~