gpt4 book ai didi

java - 将 JFrame 容器对象居中

转载 作者:行者123 更新时间:2023-12-02 14:38:16 25 4
gpt4 key购买 nike

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

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 信息(用户连接、 session 信息、权限级别等)。

好吧,现在我有了

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:将容器的父级添加到自身”。感谢您提前提供的任何帮助。

最佳答案

尝试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);
}

关于java - 将 JFrame 容器对象居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11169959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com