Java 在JFrame的特定区域放置JPanel
很抱歉没有在前一篇文章中添加特定代码。我意识到我做错了什么,但现在我又遇到了另一个问题。我试图在点击一个按钮后将一个JPanel放置在JFrame上的一个特定位置,但什么都没有发生,下面是我的Actionperformed方法的代码Java 在JFrame的特定区域放置JPanel,java,swing,user-interface,layout-manager,null-layout-manager,Java,Swing,User Interface,Layout Manager,Null Layout Manager,很抱歉没有在前一篇文章中添加特定代码。我意识到我做错了什么,但现在我又遇到了另一个问题。我试图在点击一个按钮后将一个JPanel放置在JFrame上的一个特定位置,但什么都没有发生,下面是我的Actionperformed方法的代码 public void actionPerformed(ActionEvent e) { JPanel pa=new JPanel(); frame.getContentPane().setLayout(null
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
有很多方法可以做到这一点
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
设置位置(x,y)
和设置大小(长度、宽度)
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
setBounds(x,y,长度,宽度)
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
我强烈建议你看一看。下面是每一个的链接,我为您推荐的链接旁边有一个星号(*)
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
- 用于一般方向(北、南、东、西)
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
免责声明:其中大多数是Oracle JavaDoc关于布局管理器的直接引用。请添加一些源代码作为具体示例,以便其他人能够提供帮助。现在你的问题就像“出了点问题,我不知道为什么”如果你发布代码,我会很乐意帮助你。@Tommy我在我的帖子中添加了一些代码。我对编码很陌生,所以任何帮助都会很好。@NikhilSrikumar感谢您分享您的代码。我会看看我能做些什么。你想把面板放在哪里?坐标(50150)
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}