gpt4 book ai didi

java - 如何避免一个组件另一个组件出现 "put on top of"- Java Swing

转载 作者:行者123 更新时间:2023-12-01 19:58:32 26 4
gpt4 key购买 nike

我有两个组件准备添加到框架中:

class Lamina extends JPanel{

public Lamina(){

setLayout(new BorderLayout(50,50));

JPasswordField user_password = new JPasswordField();
add(user_password, BorderLayout.SOUTH);

}

}

class DOMHeader extends JPanel
{
public DOMHeader()
{
setLayout(new BorderLayout());

JLabel title = new JLabel("Sign in");
add(title, BorderLayout.NORTH);
}
}

这是我的类(class)用户界面:

public class UI {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Frame frame = new Frame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setTitle("Metin2");

}
}

框架类:

class Frame extends JFrame {

public Frame() {
Toolkit screen = Toolkit.getDefaultToolkit();
Dimension screenSize = screen.getScreenSize();

int widthScreen = screenSize.width;
int heightScreen = screenSize.height;

setBounds((widthScreen/4/2),(heightScreen/4/2),(widthScreen/2/2), (heightScreen/2/2));

Image icon = screen.getImage("icon.jpeg");

setIconImage(icon);


/* Add to Components to frame*/
add(new DOMHeader());
add(new Lamina());

}

}

在我的 Frame 类中,我添加了前面显示的组件,但它“放置在”一个组件之上另一个组件。

根据API:

public Component add(Component comp,int index)

Adds the specified component to this container at the given position (index).

我运行主要方法:

enter image description here

如您所见,它只显示组件 DOMHeader 类:add(new DOMHeader())

add(new Lamina()) 发生了什么

我应该给它什么数字或常数?

最佳答案

这一行:

class Frame extends JFrame {

由于以下原因不正确:

  1. 由于 AWT Frame 类,这令人困惑
  2. 它扩展了 JFrame,但以后您永远不会更改 JFrame 的行为,因此无需扩展 JFrame,而是扩展 create it inside of class

现在,我们必须前往 JFrame 类,其中显示:

The default content pane will have a BorderLayout manager set on it.

现在,如果我们转到 BorderLayout 部分中布局管理器的视觉指南,我们可以看到以下图像:

enter image description here

这表明我们只能将组件添加到 5 个位置:

  • PAGE_START(或NORTH)
  • LINE_START(或WEST)
  • CENTER
  • LINE_END(或EAST)
  • PAGE_END(或SOUTH)

回答您的问题:

According to the API:

public Component add(Component comp,int index)

Adds the specified component to this container at the given position (index).

我应该给它什么数字或常数?

那么您需要按如下方式添加它:

JFrame frame = new JFrame("Write your title here"); //Follow first advice
frame.add(new DOMHeader(), BorderLayout.NORTH); //These are the constants.
frame.add(new Lamina(), BorderLayout.SOUTH);

您将这些项目添加到它们自己的 JPanel 中而不是 JFrame 本身中,这让您感到困惑。

旁注:你为什么要做这些奇怪的计算?

setBounds((widthScreen/4/2),(heightScreen/4/2),(widthScreen/2/2), (heightScreen/2/2));

如果你调用:widthScreen / 8不是更清楚吗?

<小时/>

根据 @camickr 评论:

There is no need for the DOMHeader and Lamina classes. You don't need to extend JPanel just to add a component to the frame. The "content pane" of a JFrame is a JPanel, so you can just add the label and text field to the frame as shown above.

您还可以通过以下方式改进代码:

JFrame frame = new JFrame("Your title here");
JLabel title = new JLabel("Sign in");
JPasswordField userPassword = new JPasswordField(); //changed variable name to userPassword instead of user_password to follow Java naming conventions

frame.add(title, BorderLayout.NORTH);
frame.add(userPassword, BorderLayout.SOUTH);

frame.setVisible(true); //This line should be the last one or you'll find yourself with strange "bugs" when your application starts until you move it or resize it

无需为每个组件创建全新的 JPanel

关于java - 如何避免一个组件另一个组件出现 "put on top of"- Java Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48667176/

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