我正在学习如何用 Java 创建应用程序。
我无法让 JLabel 具有背景颜色,而 JPanel 后面是白色的。另外,有没有办法将 JPanel 的大小调整为 JFrame 的一半?
任何帮助将非常感激。谢谢。
package PracticeOne;
import java.awt.BorderLayout;
public class PracticeOne {
public static void main(String[] args) {
Frame container = new Frame();
Panel box = new Panel();
Label txt = new Label();
box.add(txt);
container.add(box, BorderLayout.CENTER);
}
}
Run Code Online (Sandbox Code Playgroud)
package PracticeOne;
import javax.swing.JFrame;
public class Frame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
Frame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setTitle("Testing this out");
}
}
Run Code Online (Sandbox Code Playgroud)
package PracticeOne;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
public class Panel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public Dimension d = new Dimension(100,100);
Panel(){
this.setSize(d);
this.setAlignmentX(CENTER_ALIGNMENT);
this.setBackground(Color.WHITE);
}
}
Run Code Online (Sandbox Code Playgroud)
package PracticeOne;
import java.awt.Color;
import javax.swing.JLabel;
public class Label extends JLabel {
/**
*
*/
private static final long serialVersionUID = 1L;
Label(){
this.setSize(50, 50);
this.setText("ya boy is working here");
this.setForeground(Color.BLACK);
this.setBackground(Color.ORANGE);
}
}
Run Code Online (Sandbox Code Playgroud)
当 JPanel 为白色时,我无法让 JLabel 具有背景颜色
您需要致电setOpaque(true);
您的JLabel
另外,有没有办法将 JPanel 的大小调整为 JFrame 的一半?
您可以使用 a GridLayout
,并JPanel
在其中放置 2 ,这样,您将JPanel
使用每个 的一半大小来获得 2 JFrame
。
另外,重命名您的类,Panel
属于 AWT 中类的名称,与Frame
和相同Label
,这可能会让您(以及任何阅读您代码的人)感到困惑。
永远不要扩展JFrame
,而是基于JPanel
s 构建您的 GUI。请参阅扩展 JFrame 与在类内部创建它以及使用多个 JFrame,好的/坏的做法?普遍的共识是这很糟糕。
另外,您还应该检查我应该避免在 Swing 中使用 setPreferred|Maximum|MinimumSize() 吗?再说一遍,是的,您应该重写该getPreferredSize()
方法。
不要忘记通过更改方法将程序放置在事件调度线程 (EDT)main()
上,如下所示:
public static void main(String[] args) {
//Java 8 with lambda expressions
SwingUtilities.invokeLater(() ->
//Your code here
);
//Java 7 and below (Or 8 without lambda expressions)
SwingUtilities.invokeLater(new Runnable() {
//Your code here
});
}
Run Code Online (Sandbox Code Playgroud)
现在,根据上述所有建议,您的代码现在应该如下所示:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class HalfSizePanelWithLabelInDifferentColor {
private JFrame frame;
private Container contentPane;
private JPanel pane;
private JPanel pane2;
private JLabel label;
private static final Dimension dim = new Dimension(100, 100);
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new HalfSizePanelWithLabelInDifferentColor().createAndShowGui());
}
public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
contentPane = frame.getContentPane();
pane = new JPanel() {
@Override
public Dimension getPreferredSize() {
return dim;
}
};
pane2 = new JPanel();
pane.setOpaque(false);
pane2.setOpaque(false);
pane.setBorder(BorderFactory.createLineBorder(Color.RED));
pane2.setBorder(BorderFactory.createLineBorder(Color.BLUE));
label = new JLabel("Hello World!");
label.setBackground(Color.GREEN);
label.setOpaque(true);
contentPane.setLayout(new GridLayout(2, 1));
pane.add(label);
contentPane.add(pane);
contentPane.add(pane2);
contentPane.setBackground(Color.WHITE);
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Run Code Online (Sandbox Code Playgroud)
你的输出将是这样的:
请注意,我添加了一些彩色边框来显示一个窗格的开始和结束位置以及另一个窗格的开始和结束位置
归档时间: |
|
查看次数: |
2501 次 |
最近记录: |