我已经查看了涉及这种情况的其他问题,但它仍然为我返回0并且包只缩小了整个GUI,因此它不是原始大小。
这是我的框架窗口:
package io.ryankshah;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class ChatWindow extends JFrame
{
private JComponent loginPanel;
/**
* Creates a default chat window object
*/
public ChatWindow() {
super("Secure Client/Server Chat");
loginPanel = new LoginPanel(this);
setSize(new Dimension(800, 600));
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addComponents();
pack();
setLocationRelativeTo(null);
setVisible(true);
}
/**
* Adds all chat-related components to the main frame
*/
private void addComponents() {
add(loginPanel);
}
public static void main(String[] args) {
new ChatWindow();
}
}
最后这是我的LoginPanel,其中调用了getContentPane()。getWidth():
package io.ryankshah;
import java.awt.Graphics;
import java.io.DataOutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginPanel extends JPanel
{
private List<JComponent> loginComponents;
private JTextField username;
private JPasswordField password;
private JButton login;
private static final String USER_AGENT = "Mozilla/5.0";
private static final int INPUT_WIDTH = 400;
private static final int INPUT_HEIGHT = 30;
/**
* Creates a default login panel object
* @param parent The parent container of the panel
*/
public LoginPanel(JFrame parent) {
setLayout(null);
loginComponents = new ArrayList<JComponent>();
username = new JTextField();
password = new JPasswordField();
login = new JButton("Login");
loginComponents.add(username);
loginComponents.add(password);
loginComponents.add(login);
int j = parent.getContentPane().getHeight();
int componentCenter = (parent.getWidth() / 2) - (INPUT_WIDTH / 2);
for(int i = 0; i < loginComponents.size(); i++) {
JComponent jc = loginComponents.get(i);
System.out.println(parent.getContentPane().getWidth());
jc.setBounds(componentCenter, j % (i + 1), INPUT_WIDTH, INPUT_HEIGHT);
add(jc);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
@Override
public void update(Graphics g) {
super.update(g);
}
//TODO: create case method for getting any field value
/**
* Gets the username from the username field
* @return Returns the username from the username field
*/
public String getUsername() {
return username.getText();
}
/**
* Gets the password from the password field
* @return Returns the password from the password field
*/
public String getPassword() {
return new String(password.getPassword());
}
/**
* Creates a login request to the server returns the result
* @param url URL the request is sent to
* @return result Result of the request
*/
public void createLoginRequest(String url) throws Exception {
URL urlObj = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection) urlObj.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
connection.setDoOutput(true);
String params = "username="+getUsername()+"&password="+getPassword();
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
dos.writeBytes(params);
dos.flush();
dos.close();
}
}
答案 0 :(得分:7)
在回复您的评论时,请致电:
loginPanel = new LoginPanel(this);
您正在传递未显示的JFrame
。所以在这一点上,它的高度和宽度都是0。
在LoginPanel
课程中,当您致电:
int componentCenter = (parent.getWidth() / 2) - (INPUT_WIDTH / 2);
parent.getWidth() / 2
将使用0。
这将导致您的边界设置为负值。这可能不是你想要的