English 中文(简体)
向 JApplet 添加带有空布局的 JApplet 简纸
原标题:Adding JPanels to the JApplet with a null layout
  • 时间:2012-05-24 11:57:45
  •  标签:
  • java
  • swing

我想为我拥有的JApplet增加4张纸币,我给每个纸币一个不同的颜色。但是没有显示任何颜色 - 我的意思是我看不到输出。 根本没有颜色。 以下代码在 init () 方法中 。

  this.setSize(1400, 780);
      this.setVisible(true);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setLocationRelativeTo(null);
  this.setLayout(null) ; 

      setLayout(null) ; 

  Panel1 = new JPanel() ;
  Panel2 = new JPanel () ; 
  Panel3 = new JPanel() ; 
  Panel4 = new JPanel() ; 

  Label1 = new JLabel ("Label1") ; 
  Label2 = new JLabel ("Label2") ; 
  Label3 = new JLabel ("Label3") ; 
  Label4 = new JLabel ("Label4") ; 

  Panel1.add(Label1) ; 
  Panel2.add(Label2) ; 
  Panel3.add(Label3) ; 
  Panel4.add(Label4) ; 

  // Panel 1 "About Me"
  Panel1.setSize(140,390) ; 
  Panel1.setLocation(0,0) ; 
  Panel1.setBackground(Color.red) ; 
  Panel1.setVisible(true) ; 
  this.add(Panel1) ; 

  // Panel 2 "MyHoppies" 
  Panel2.setSize(140,390) ; 
  Panel2.setLocation(0,700) ; 
  Panel2.setBackground(Color.yellow) ;
  this.add(Panel2) ; 

  // Panel 3 "Photo Gallery"
  Panel3.setSize(140,390) ; 
  Panel3.setLocation(390,0) ; 
  Panel3.setBackground(Color.black) ;
  this.add(Panel3) ;

  // Panel 4 "Happey face" 
  Panel4.setSize(140,390) ;
  Panel4.setLocation(390,700) ; 
  Panel4.setBackground(Color.pink) ; 
  this.add(Panel4) ; 
最佳回答
  • <代码代码代码> this.set可视( true) 必须是 GUI 构造器中的最后一条代码行

  • http://java.about.com/od/javasyntax/a/nameconventions.htm" rel=“noreferrer” >Using Java命名公约 正确。 那么,<代码代码代码>Panel1 应该是 <代码代码代码>panel1 e.i。

  • 不延伸 <代码代码代码> JFrame 或 <代码代码代码> JApplet , 创建本地变量, 与 <代码代码代码> Panel1 的本地变量相同

  • 不要使用 <代码代码代码> nullLayout , 使用适当的 <代码代码代码> LayoutManager , 代之以, 在这种情况下, <代码代码代码> gridLayoot 可能, 否则 <代码代码代码> JFrames 内容与 <代码代码代码> JFrame 无法重新连接

""https://i.sstatic.net/ehyVP.jpg" alt="此处的内置图像描述"/ >

代码代码代码

<代码代码代码>import java.awt.*;
import javax.swing.*;

public class ColorongPanels {

    private JFrame frame = new JFrame("ColorongPanels");
    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private JPanel panel3 = new JPanel();
    private JPanel panel4 = new JPanel();
    private JLabel label1 = new JLabel("Label1");
    private JLabel label2 = new JLabel("Label2");
    private JLabel label3 = new JLabel("Label3");
    private JLabel label4 = new JLabel("Label4");

    public ColorongPanels() {
        frame.setLayout(new GridLayout(2, 2, 5, 5));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel1.setBackground(Color.red);
        panel1.setLayout(new BorderLayout());
        panel1.add(label1, BorderLayout.CENTER);
        panel2.setBackground(Color.yellow);
        panel2.setLayout(new BorderLayout());
        panel2.add(label2, BorderLayout.CENTER);
        panel3.setBackground(Color.black);
        panel3.setLayout(new BorderLayout());
        panel3.add(label3, BorderLayout.CENTER);
        panel4.setBackground(Color.pink);
        panel4.setLayout(new BorderLayout());
        panel4.add(label4, BorderLayout.CENTER);
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);
        frame.add(panel4);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ColorongPanels();
            }
        });
    }
}
问题回答

You should do this:
panelx.setOpaque(true) and it should work

首先: 您需要 JPanuel. setOpaque( true) 面板才能查看背景颜色 。

第二:背景颜色属性在不同平台上具有不同效果。 例如: 如果您设置了 JButton 的背景, 您将会看到 Win7 中的按钮颜色, 但不在 WinXP (不确定其他操作系统下如何使用) 。 这至少是我的经验...





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签