如何避免;放在“上”;一个组件另一个组件-JavaSwing
我准备将两个组件添加到框架中:如何避免;放在“上”;一个组件另一个组件-JavaSwing,java,swing,Java,Swing,我准备将两个组件添加到框架中: class Lamina extends JPanel{ public Lamina(){ setLayout(new BorderLayout(50,50)); JPasswordField user_password = new JPasswordField(); add(user_password, BorderLayout.SOUTH); } } class DOMHeader e
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);
}
}
这是我的班级界面:
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());
}
}
在我的类框架中,我添加了前面显示的组件,但它“放在”一个组件和另一个组件之上
根据API:
public Component add(Component comp,int index)
public Component add(Component comp,int index)
将指定的组件添加到此容器的给定位置
(索引)
我运行主方法:
正如您所看到的,它只显示组件DOMHeader
class:add(newdomHeader())
添加(新薄板())
我应该给它什么数字或常数?这一行:
class Frame extends JFrame {
不正确,原因如下:
JFrame
,但您以后永远不会更改JFrame
的行为,因此无需扩展JFrame
,但是
(或PAGE\u START
)NORTH
(或LINE\u开始
)WEST
CENTER
(或LINE\u END
)EAST
(或PAGE\u END
)SOUTH
public Component add(Component comp,int index)
public Component add(Component comp,int 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
s中的位置,而不是添加到JFrame
本身,这让您感到困惑
旁注:你为什么要做这些奇怪的计算
setBounds((widthScreen/4/2),(heightScreen/4/2),(widthScreen/2/2), (heightScreen/2/2));
如果您调用:widthScreen/8
,不是会更清楚吗
根据@camickr评论:
不需要DOMHeader和Lava类。您不需要扩展JPanel,只需将组件添加到框架中即可。JFrame的“内容窗格”是一个JPanel,因此您只需将标签和文本字段添加到框架中,如上所示 您还可以通过以下方式改进代码:
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
。此行:
class Frame extends JFrame {
不正确,原因如下:
JFrame
,但您以后永远不会更改JFrame
的行为,因此无需扩展JFrame
,但是
(或PAGE\u START
)NORTH
(或LINE\u开始
)WEST
CENTER
(或LINE\u END
)EAST
(或PAGE\u END
)SOUTH
public Component add(Component comp,int index)
public Component add(Component comp,int 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
s中的位置,而不是添加到JFrame
本身,这让您感到困惑
旁注:你为什么要做这些奇怪的计算
setBounds((widthScreen/4/2),(heightScreen/4/2),(widthScreen/2/2), (heightScreen/2/2));
如果您调用:widthScreen/8
,不是会更清楚吗
根据@camickr评论:
不需要DOMHeader和Lava类。您不需要扩展JPanel,只需将组件添加到框架中即可。JFrame的“内容窗格”是一个JPanel,因此您只需将标签和文本字段添加到框架中,如上所示 您还可以通过以下方式改进代码:
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
。为了尽快获得更好的帮助,请发布或。使用空布局将在0,0处添加每个组件,您应该使用布局管理器,例如AxisLayout,索引仅用于确定首先绘制哪个组件(第一个位于其他组件的底部)@MarcosVasconcelos他没有使用null布局
。他刚刚在JFrame
本身上调用了setBounds(…)
,将其定位在屏幕上。为了更快地获得更好的帮助,发布or。使用null布局会将每个组件添加到0,0,您应该使用布局管理器,例如AxisLayout,索引仅用于确定哪个组件是首先绘制的(第一个位于其他组件的底部)@MarcosVasconcelos他没有使用null布局
。他只是在JFrame
本身上调用了setBounds(…)
,将其定位到屏幕上,不需要DOMHeader和薄层类。您不需要扩展JPanel,只需将组件添加到框架中即可。JFrame的“内容窗格”是一个JPanel,因此您只需将标签和文本字段添加到框架中,如上图所示。Nice catch@camickr我只关注主要问题,没有注意到它。我只是在你的评论后添加了一个编辑:)谢谢@Frakcool,我为我的错误代码感到抱歉,我是初学者。你的答案很有用,一点也不差,但它可以改进,我们都是初学者,所以要坚持下去。只需注意一些建议,这样你就可以提高:)干杯@jjoselonThere不需要使用DOMHeader和lama类。您不需要扩展JPanel,只需将组件添加到框架中即可。JFrame的“内容窗格”是一个JPanel,因此您只需将标签和文本字段添加到框架中,如上图所示