在JavaSwing中制作第二帧
我已经用Eclipse在JavaSwing中制作了一张简单的数字名片,我对Java和编程都是全新的,所以请耐心听我说。基本上,我想做的是添加一个按钮,上面写着“我们的工作”或“公文包”,例如,单击该按钮将打开一个新窗口,在那里我可以添加图片、链接或虚假评论等 我知道这可能很简单,但当它基于其他人的代码时,我很难理解很多教程在JavaSwing中制作第二帧,java,swing,frame,frames,Java,Swing,Frame,Frames,我已经用Eclipse在JavaSwing中制作了一张简单的数字名片,我对Java和编程都是全新的,所以请耐心听我说。基本上,我想做的是添加一个按钮,上面写着“我们的工作”或“公文包”,例如,单击该按钮将打开一个新窗口,在那里我可以添加图片、链接或虚假评论等 我知道这可能很简单,但当它基于其他人的代码时,我很难理解很多教程 package dbuscard; import java.awt.EventQueue; import javax.swing.JFrame; import java.
package dbuscard;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JSeparator;
public class card {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
card window = new card();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public card() {
initialize();
}
/**
* Initialise the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(153, 204, 255));
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblLogo = new JLabel("");
lblLogo.setBounds(127, 11, 219, 104);
frame.getContentPane().add(lblLogo);
lblLogo.setIcon(new ImageIcon("Images\\zlogoimg.png"));
JLabel lblNewLabel = new JLabel("t: 01254 777494");
lblNewLabel.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblNewLabel.setBounds(20, 187, 126, 14);
frame.getContentPane().add(lblNewLabel);
JLabel lblEApexuxdesigngmailcom = new JLabel("e: apexuxdesign@gmail.com");
lblEApexuxdesigngmailcom.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblEApexuxdesigngmailcom.setBounds(20, 204, 200, 14);
frame.getContentPane().add(lblEApexuxdesigngmailcom);
JLabel lblVisitWebsite = new JLabel("visit website");
lblVisitWebsite.setFont(new Font("Source Code Pro Light", Font.BOLD, 11));
lblVisitWebsite.setBounds(10, 237, 117, 14);
frame.getContentPane().add(lblVisitWebsite);
JLabel facebook = new JLabel("");
facebook.setBounds(282, 204, 64, 47);
frame.getContentPane().add(facebook);
facebook.setIcon(new ImageIcon("Images\\facebook.png"));
JLabel twitter = new JLabel("");
twitter.setBounds(320, 204, 72, 47);
frame.getContentPane().add(twitter);
twitter.setIcon(new ImageIcon("Images\\twitter.png"));
JLabel youtube = new JLabel("");
youtube.setBounds(356, 204, 68, 47);
frame.getContentPane().add(youtube);
youtube.setIcon(new ImageIcon("Images\\youtube.png"));
JLabel lblSeanHutchinson = new JLabel("Sean Hutchinson");
lblSeanHutchinson.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblSeanHutchinson.setBounds(20, 128, 126, 14);
frame.getContentPane().add(lblSeanHutchinson);
JLabel lblUxDesigner = new JLabel("UX Designer");
lblUxDesigner.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblUxDesigner.setBounds(20, 145, 107, 14);
frame.getContentPane().add(lblUxDesigner);
JLabel lblNewLabel_1 = new JLabel("CEO - Apex UX Design");
lblNewLabel_1.setFont(new Font("Source Code Pro Light", Font.PLAIN, 11));
lblNewLabel_1.setBounds(20, 162, 158, 14);
frame.getContentPane().add(lblNewLabel_1);
}
}
简单。添加使用方法
setVisible(true)的操作侦听器代码>在单独的JFrame上。示例代码:
package com.nonsense;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class del {
private JFrame frame, frame2;
/**
* Launch the application.
*/
public static void main(String[] args) {
del window = new del();
window.frame.setVisible(true);
}
/**
* Create the application.
*/
public del() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame2 = new JFrame();
frame2.setBounds(100, 100, 450, 300);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame2.getContentPane().setLayout(null);
JButton btnOpenWindow = new JButton("Open Window");
btnOpenWindow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame2.setVisible(true);
}
});
btnOpenWindow.setBounds(167, 118, 120, 23);
frame.getContentPane().add(btnOpenWindow);
}
}
我使用“DISPOSE\u ON\u CLOSE”(在关闭时释放),这样当按下X按钮时,第二个窗口不会终止程序。我还没有尝试过这样做,但我相信这就像有两个JFrame一样简单JFrame主窗口,portfolioWindow
并使用信息对两者进行初始化。请查看这是一个坏主意的原因,以及更合理的解决方案。事实上,这与Swing 101差不多,请参阅以了解更多详细信息。避免使用null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing设计的核心是与布局管理器一起工作,丢弃它们将导致无止境的问题,您将花费越来越多的时间来纠正这些问题“当它基于其他人的代码时,我很难理解很多教程。”。。我不明白我们怎么能帮助你。因此,这不是一个寻找私人导师的地方代码>…嗯,讽刺的是很恰当。1) 请参见2)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。3) Swing/AWT GUI应该在EDT上启动。@Andrew Thompson有很多优点。但是,我无法在不同的操作系统上进行测试。这一个在Win8上运行,适用于任何想知道的人。“我不能在不同的操作系统上测试。”我也不能。。这就是为什么把它留给布局很有意义的原因之一@安德鲁·汤普森:嗯,这是一个很好的观点。希望肖恩能得到帮助:)