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