Java GUI不出现
我有一个Main类,它有一个公共的静态void Main(String[]args){}。我还有一个叫做appGUI的类。我一直在尝试在运行Main类时加载GUI,但什么都没有发生,甚至没有任何错误…:( 以下是主要课程:Java GUI不出现,java,swing,jframe,visible,Java,Swing,Jframe,Visible,我有一个Main类,它有一个公共的静态void Main(String[]args){}。我还有一个叫做appGUI的类。我一直在尝试在运行Main类时加载GUI,但什么都没有发生,甚至没有任何错误…:( 以下是主要课程: public class Main { /** * @param args */ public static void main(String[] args) throws Exception { appGUI
public class Main {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
appGUI gui = new appGUI();
}
}
下面是appGUI类:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
* Created by JFormDesigner on Wed Apr 03 19:24:35 BST 2013
*/
/**
* @author Hrach Ghapantsyan
*/
public class appGUI extends JFrame {
public appGUI() {
initComponents();
}
private void loginButtonActionPerformed(ActionEvent e) {
// TODO add your code here
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner Evaluation license - Hrach Ghapantsyan
loginPasswordField = new JPasswordField();
loginUsernameField = new JTextField();
usernameLabel = new JLabel();
passwordLabel = new JLabel();
loginButton = new JButton();
titleLabel = new JLabel();
//======== this ========
setTitle("Experimental X | Administrator Login");
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.add(loginPasswordField);
loginPasswordField.setBounds(80, 65, 100, loginPasswordField.getPreferredSize().height);
contentPane.add(loginUsernameField);
loginUsernameField.setBounds(80, 35, 100, loginUsernameField.getPreferredSize().height);
//---- usernameLabel ----
usernameLabel.setText("Username:");
contentPane.add(usernameLabel);
usernameLabel.setBounds(20, 40, 55, usernameLabel.getPreferredSize().height);
//---- passwordLabel ----
passwordLabel.setText("Password:");
contentPane.add(passwordLabel);
passwordLabel.setBounds(20, 70, 55, passwordLabel.getPreferredSize().height);
//---- loginButton ----
loginButton.setText("Login");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loginButtonActionPerformed(e);
}
});
contentPane.add(loginButton);
loginButton.setBounds(80, 95, 100, loginButton.getPreferredSize().height);
//---- titleLabel ----
titleLabel.setText("Experimental X | Administrator Login");
contentPane.add(titleLabel);
titleLabel.setBounds(45, 10, 190, titleLabel.getPreferredSize().height);
{ // compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < contentPane.getComponentCount(); i++) {
Rectangle bounds = contentPane.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = contentPane.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
contentPane.setMinimumSize(preferredSize);
contentPane.setPreferredSize(preferredSize);
}
setSize(270, 170);
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// Generated using JFormDesigner Evaluation license - Hrach Ghapantsyan
private JPasswordField loginPasswordField;
private JTextField loginUsernameField;
private JLabel usernameLabel;
private JLabel passwordLabel;
private JButton loginButton;
private JLabel titleLabel;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
/*
*由JFormDesigner于2013年4月3日星期三19:24:35创建
*/
/**
*@作者Hrach Ghapantsyan
*/
公共类appGUI扩展了JFrame{
公共appGUI(){
初始化组件();
}
已执行私有void登录按钮操作(ActionEvent e){
//TODO在此处添加代码
}
私有组件(){
//JFormDesigner-组件初始化-不要修改//GEN-BEGIN:initComponents
//使用JFormDesigner评估许可证生成-Hrach Ghapantsyan
loginPasswordField=新的JPasswordField();
loginUsernameField=newjtextfield();
usernamelab=newjlabel();
passwordLabel=new JLabel();
loginButton=新JButton();
titleLabel=新的JLabel();
//============这个========
setTitle(“实验X |管理员登录”);
容器contentPane=getContentPane();
contentPane.setLayout(null);
添加(loginPasswordField);
loginPasswordField.setBounds(80,65,100,loginPasswordField.getPreferredSize().height);
contentPane.add(loginUsernameField);
setBounds(80,35,100,loginUsernameField.getPreferredSize().height);
//----用户名标签----
usernamelab.setText(“用户名:”);
contentPane.add(usernamelab);
setBounds(20,40,55,usernamelab.getPreferredSize().height);
//----密码标签----
passwordLabel.setText(“密码:”);
contentPane.add(密码标签);
passwordLabel.setBounds(20,70,55,passwordLabel.getPreferredSize().height);
//----登录按钮----
setText(“登录”);
addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
执行登录按钮操作(e);
}
});
添加(登录按钮);
setBounds(80,95,100,loginButton.getPreferredSize().height);
//----标题标签----
titleLabel.setText(“实验X |管理员登录”);
contentPane.add(标题标签);
titleLabel.setBounds(45,10190,titleLabel.getPreferredSize().height);
{//计算首选大小
维度preferredSize=新维度();
对于(int i=0;i
我曾尝试在Eclipse和netbeans上运行main类,但它运行几秒钟后就停止了。我没有收到任何错误。在座各位有什么建议吗?谢谢。您没有调用
JFrame\setVisible
:
gui.setVisible(true);
一些注意事项:
- 避免使用
布局。始终使用null
- 不要使用setXXXSize方法。重写
方法以确定组件大小getPreferredSize
- 您通常希望创建一个JFrame并直接使用它,而不是扩展JFrame
- 考虑使用创建
中的EDT
JFrame
public class appGUI extends JFrame {
setVisible(true);
// ----
}
放置此代码以使其可见。只需添加:
this.setVisible(true);
非常感谢,但我用了另一种方式:appGUI=newappgui();gui.setVisible(true);