java SWT 如何在一个窗口内通过按钮切换界面
1个回答
2016-05-22 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
第一个窗体LoginFrame.java:
package winRelation;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class LoginFrame extends JFrame {
JButton button = new JButton("点击我");
class LoginOKAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "将进入另一个窗体!");
new MainFrame();
setVisible(false);
}
}
public LoginFrame(){
super();
this.setResizable(false);
this.setSize(new Dimension(300, 205));
this.setTitle("第一个窗体");
this.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocation(300, 200);
this.setVisible(true);
this.getContentPane().add(button, null);
button.setBounds(new Rectangle(111, 70, 78, 27));
button.addActionListener(new LoginOKAction());//给按钮加事件监听器
}
public static void main(String[] args) {
new LoginFrame();
}
}
第二个窗体MainFrame.java:
package winRelation;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
public class MainFrame extends JFrame{
private static final JDesktopPane DESKTOP_PANE = new JDesktopPane();
public MainFrame() {
super("这是主窗体");
setSize(640, 480);
//菜单设置
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
JMenu menu1 = new JMenu("菜单1");
JMenu menu101 = new JMenu("菜单101");
JMenuItem menu10101 = new JMenuItem("菜单10101");
JMenuItem menu102 = new JMenuItem("菜单102");
menu102.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addIFame(new InternalFrame());
}
});
menu101.add(menu10101);
menu1.add(menu101);
menu1.add(menu102);
JMenu menu2 = new JMenu("菜单2");
menuBar.add(menu1);
menuBar.add(menu2);
this.getContentPane().add(DESKTOP_PANE);
this.setVisible(true);
}
public static void addIFame(JInternalFrame iframe) { // 添加子窗体的方法
DESKTOP_PANE.add(iframe);
}
public static void main(String[] args) {
new MainFrame();
}
}
第三个窗体(MainFrame中的窗体)InternalFrame.java:
package winRelation;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
public class InternalFrame extends JInternalFrame{
public InternalFrame() {
super();
setClosable(true);
setIconifiable(true);
setTitle("内部窗体");
setBounds(50,50,400,300);
setVisible(true);
}
}
package winRelation;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class LoginFrame extends JFrame {
JButton button = new JButton("点击我");
class LoginOKAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "将进入另一个窗体!");
new MainFrame();
setVisible(false);
}
}
public LoginFrame(){
super();
this.setResizable(false);
this.setSize(new Dimension(300, 205));
this.setTitle("第一个窗体");
this.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocation(300, 200);
this.setVisible(true);
this.getContentPane().add(button, null);
button.setBounds(new Rectangle(111, 70, 78, 27));
button.addActionListener(new LoginOKAction());//给按钮加事件监听器
}
public static void main(String[] args) {
new LoginFrame();
}
}
第二个窗体MainFrame.java:
package winRelation;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
public class MainFrame extends JFrame{
private static final JDesktopPane DESKTOP_PANE = new JDesktopPane();
public MainFrame() {
super("这是主窗体");
setSize(640, 480);
//菜单设置
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
JMenu menu1 = new JMenu("菜单1");
JMenu menu101 = new JMenu("菜单101");
JMenuItem menu10101 = new JMenuItem("菜单10101");
JMenuItem menu102 = new JMenuItem("菜单102");
menu102.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addIFame(new InternalFrame());
}
});
menu101.add(menu10101);
menu1.add(menu101);
menu1.add(menu102);
JMenu menu2 = new JMenu("菜单2");
menuBar.add(menu1);
menuBar.add(menu2);
this.getContentPane().add(DESKTOP_PANE);
this.setVisible(true);
}
public static void addIFame(JInternalFrame iframe) { // 添加子窗体的方法
DESKTOP_PANE.add(iframe);
}
public static void main(String[] args) {
new MainFrame();
}
}
第三个窗体(MainFrame中的窗体)InternalFrame.java:
package winRelation;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
public class InternalFrame extends JInternalFrame{
public InternalFrame() {
super();
setClosable(true);
setIconifiable(true);
setTitle("内部窗体");
setBounds(50,50,400,300);
setVisible(true);
}
}
追问
SWT,不要awt和swing
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询