2

每次我创建一个新的 JFrame 对象时,框架都会出现在 0,0 位置。我尝试使用 setLocationRelativeTo() 查找如何使窗口居中,但我尝试在容器上调用的所有内容都显示为不可编译的代码。这是我正在使用的课程之一:

package Ginfo;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class EditUsers extends JFrame implements ActionListener
{
    Container EU;

    JPanel modUsers, button, output;
    JLabel user, password;
    JTextField tuser, tpassword;
    JTextArea toutput;
    JButton addAdmin, addStand, editPass, removeUser, exit;

    ObjectOutputStream oout;
    ObjectInputStream oin;

    Message m;
    ConnectInfo c;

public EditUsers (ObjectOutputStream oout2, ObjectInputStream oin2, Message m2, ConnectInfo a)
{
    super("User Modifications");
    EU = getContentPane();
    oout = oout2;
    try 
    {
        oout.reset();
    } 
    catch (IOException e)
    {
        e.printStackTrace();
    }
    oin = oin2;
    m = m2;
    c = a;

    buildUserInfoPanel();
    buildOutputPanel();
    buildButtonPanel();
    EU.add(modUsers, BorderLayout.NORTH);
    EU.add(output, BorderLayout.CENTER);
    EU.add(button, BorderLayout.SOUTH);
    pack();
    setVisible(true);

}

private void buildUserInfoPanel()
{
    modUsers = new JPanel();
    modUsers.setLayout(new GridLayout (2,2));
    user = new JLabel ("Enter the username you wish to edit: ");
    tuser = new JTextField (15);
    password = new JLabel("Enter their new password: ");
    tpassword = new JTextField(15);

    modUsers.add(user);
    modUsers.add(tuser);
    modUsers.add(password);
    modUsers.add(tpassword);
}

private void buildOutputPanel()
{
    output = new JPanel();
    toutput = new JTextArea();
    toutput.setPreferredSize(new Dimension (400,100));

    output.add(toutput);
}

private void buildButtonPanel()
{
    button = new JPanel();
    addAdmin = new JButton ("Add user as administrator");
    addStand = new JButton ("Add user as standard");
    editPass = new JButton ("Change password");
    removeUser = new JButton ("Remove user");
    exit = new JButton ("Back");

    button.add(addAdmin);
    button.add(addStand);
    button.add(editPass);
    button.add(removeUser);
    button.add(exit);

    addAdmin.addActionListener(this);
    addStand.addActionListener(this);
    editPass.addActionListener(this);
    removeUser.addActionListener(this);
    exit.addActionListener(this);
}

public void actionPerformed(ActionEvent e) 
{
    if (e.getSource() == addAdmin)
    {
        m.type = Message.ADDADMIN;
        m.main = tuser.getText();
        m.setPassword = tpassword.getText();
        m.setPermission = 1;
    }
    else if (e.getSource() == addStand)
    {
        m.type = Message.ADDSTANDARD;
        m.main = tuser.getText();
        m.setPassword = tpassword.getText();
        m.setPermission = 2;
    }
    else if (e.getSource() == editPass)
    {
        m.type = Message.CHANGEPASSWORD;
        m.main = tuser.getText();
        m.setPassword = tpassword.getText();
    }
    else if (e.getSource() == removeUser)
    {
        m.type = Message.REMOVEUSER;
        m.main = tuser.getText();
    }
    else if (e.getSource() == exit)
    {
        new WhatToDo (oout, oin, m, c);
        dispose();
        return;
    }
    try 
    {
        oout.writeObject(m);
        m = (Message)oin.readObject();
        toutput.setText(m.response);
    } 
    catch (IOException e1) 
    {
        e1.printStackTrace();
    } 
    catch (ClassNotFoundException e1) 
    {
        e1.printStackTrace();
    }
}
}

当我运行此代码时,窗口从 (0,0) 位置开始。我想做的是将窗口大小调整为运行它的计算机分辨率大小的 1/4,并重置窗口的位置,使窗口的中心位于屏幕的中心。如果分辨率为 1000x1000,则窗口应为 250x250,并位于 (500 - 250/2)X,(500 - 250/2)Y(左上角)乘 (500 + 250/2)X, (500 + 250/2) )Y(右下角)

(请注意,我为每个窗口使用不同的类,我在进程完成时打开和关闭它,所以如果没有其他类,这将无法完全编译。我正在传递我的 ObjectOutputStream、ObjectInputStream、我的对象调用 Message,另一个我称为 ConnectInfo 的对象。输出和输入流是为了保持与服务器的连接,Message 是来回发送的序列化信息,ConnectionInfo 主要保存 cookie 信息(用户连接、会话信息、权限水平等)。

好的,现在我有

package Ginfo;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class EditUsers implements ActionListener
{

Container EU;

JFrame myJFrame;

JPanel modUsers, button, output;
JLabel user, password;
JTextField tuser, tpassword;
JTextArea toutput;
JButton addAdmin, addStand, editPass, removeUser, exit;

ObjectOutputStream oout;
ObjectInputStream oin;

Message m;
ConnectInfo c;

public EditUsers (ObjectOutputStream oout2, ObjectInputStream oin2, Message m2, ConnectInfo a)
{
    myJFrame = new JFrame("User Modifications");
    EU = myJFrame.getContentPane();
    oout = oout2;
    try 
    {
        oout.reset();
    } 
    catch (IOException e)
    {
        e.printStackTrace();
    }
    oin = oin2;
    m = m2;
    c = a;

    buildUserInfoPanel();
    buildOutputPanel();
    buildButtonPanel();
    EU.add(modUsers, BorderLayout.NORTH);
    EU.add(output, BorderLayout.CENTER);
    EU.add(button, BorderLayout.SOUTH);
    myJFrame.add(EU);
    myJFrame.pack();
    myJFrame.setVisible(true);

}

private void buildUserInfoPanel()
{
    modUsers = new JPanel();
    modUsers.setLayout(new GridLayout (2,2));
    user = new JLabel ("Enter the username you wish to edit: ");
    tuser = new JTextField (15);
    password = new JLabel("Enter their new password: ");
    tpassword = new JTextField(15);

    modUsers.add(user);
    modUsers.add(tuser);
    modUsers.add(password);
    modUsers.add(tpassword);
}

private void buildOutputPanel()
{
    output = new JPanel();
    toutput = new JTextArea();
    toutput.setPreferredSize(new Dimension (200,100));

    output.add(toutput);
}

private void buildButtonPanel()
{
    button = new JPanel();
    addAdmin = new JButton ("Add user as administrator");
    addStand = new JButton ("Add user as standard");
    editPass = new JButton ("Change password");
    removeUser = new JButton ("Remove user");
    exit = new JButton ("Back");

    button.add(addAdmin);
    button.add(addStand);
    button.add(editPass);
    button.add(removeUser);
    button.add(exit);

    addAdmin.addActionListener(this);
    addStand.addActionListener(this);
    editPass.addActionListener(this);
    removeUser.addActionListener(this);
    exit.addActionListener(this);
}

public void actionPerformed(ActionEvent e) 
{
    if (e.getSource() == addAdmin)
    {
        m.type = Message.ADDADMIN;
        m.main = tuser.getText();
        m.setPassword = tpassword.getText();
        m.setPermission = 1;
    }
    else if (e.getSource() == addStand)
    {
        m.type = Message.ADDSTANDARD;
        m.main = tuser.getText();
        m.setPassword = tpassword.getText();
        m.setPermission = 2;
    }
    else if (e.getSource() == editPass)
    {
        m.type = Message.CHANGEPASSWORD;
        m.main = tuser.getText();
        m.setPassword = tpassword.getText();
    }
    else if (e.getSource() == removeUser)
    {
        m.type = Message.REMOVEUSER;
        m.main = tuser.getText();
    }
    else if (e.getSource() == exit)
    {
        new WhatToDo (oout, oin, m, c);
        myJFrame.dispose();
        return;
    }
    try 
    {
        oout.writeObject(m);
        m = (Message)oin.readObject();
        toutput.setText(m.response);
    } 
    catch (IOException e1) 
    {
        e1.printStackTrace();
    } 
    catch (ClassNotFoundException e1) 
    {
        e1.printStackTrace();
    }
}
}

现在问题出在 myJFrame.add(EU) 上,它说“java.lang.IllegalArgumentException:将容器的父级添加到自身”。感谢您提前提供任何帮助。

4

3 回答 3

7

尝试myJFrameObject.setLocationRelativeTo(null);

来自 JavaDoc

  • 如果该组件为空,或者与该组件关联的 GraphicsConfiguration 为空,则窗口被放置在屏幕的中心。中心点可以通过 GraphicsEnvironment.getCenterPoint 方法获得。
  • 如果组件不为 null,但当前未显示,则窗口将放置在与此组件关联的 GraphicsConfiguration 定义的目标屏幕的中心。
  • 如果组件不为空并且显示在屏幕上,则窗口的位置应使窗口的中心与组件的中心重合。

编辑

我刚刚在这个(不太高级的)构造函数上测试了你的代码,它似乎像你想要的那样工作(至少这是我希望的)。setLocationRelativeTo(null)我刚刚放置setVisible(true)以确保 Frame 将包含其所有元素并知道居中时使用的大小。

public EditUsers() {
    super("User Modifications");
    //I removed some not GUI operations

    EU = getContentPane();

    buildUserInfoPanel();
    buildOutputPanel();
    buildButtonPanel();
    EU.add(modUsers, BorderLayout.NORTH);
    EU.add(output, BorderLayout.CENTER);
    EU.add(button, BorderLayout.SOUTH);
    pack();
    //put setLocationRelativeTo before setVisible() 
    setLocationRelativeTo(null);
    setVisible(true);
}
于 2012-06-23T13:46:56.490 回答
5

您必须在 GUI 构造函数中调用最后的代码行

JFrame#pack();
JFrame#setLocationRelativeTo(null); // Windows OS
//or JFrame#setLocationByPlatform(true); //depends of Native OS
JFrame#setVisible(true);

另一个建议

编辑

将代码行EditUsers eu = new EditUsers();放在主类中,返回新的 GUI 实例JFrame,包装代码创建 GUI 到InitialThread,别无所求:-)

   public static void main(String[] a_args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                EditUsers eu = new EditUsers();
            }
        });
    }

编辑2

修改后的代码返回

在此处输入图像描述

从代码

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class EditUsers implements ActionListener {

    private JFrame myJFrame;
    private JPanel modUsers, button, output;
    private JLabel user, password;
    private JTextField tuser, tpassword;
    private JTextArea toutput;
    private JButton addAdmin, addStand, editPass, removeUser, exit;
    private ObjectOutputStream oout;
    private ObjectInputStream oin;
    //private Message m;
    //private ConnectInfo c;

    public static void main(String[] a_args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                EditUsers eu = new EditUsers();
            }
        });
    }

    public EditUsers() {
        //editUsers(oout2, oin2, m2, a);
        buildUserInfoPanel();
        buildOutputPanel();
        buildButtonPanel();
        myJFrame = new JFrame("User Modifications");
        myJrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myJFrame.add(modUsers, BorderLayout.NORTH);
        myJFrame.add(output, BorderLayout.CENTER);
        myJFrame.add(button, BorderLayout.SOUTH);
        myJFrame.pack();
        myJFrame.setLocationRelativeTo(null);
        myJFrame.setVisible(true);
    }

    /*private void editUsers(ObjectOutputStream oout2, 
          ObjectInputStream oin2, Message m2, ConnectInfo a) {
        oout = oout2;
        try {
            oout.reset();
        } catch (IOException e) {
            e.printStackTrace();
        }
        oin = oin2;
        m = m2;
        c = a;
    }*/

编辑 3

JTextArea应该在JScrollPane中,

于 2012-06-23T13:44:37.233 回答
2

应该在setLocationRelativeTo窗口的尺寸或多或少初始化后调用。

关于您在pack命令之后直接采样。该pack()命令可能会重新排列您的框架。

于 2012-06-23T14:14:16.893 回答