我正在学习如何用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);
}
}
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");
}
}
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);
}
}
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);
}
}
答案 0 :(得分:2)
我无法让JLabel在JPanel为白色时拥有背景色
您需要在setOpaque(true);
JLabel
另外,有没有办法将JPanel的大小调整为JFrame的一半?
您可以使用GridLayout
,并在其中放置2个JPanel
,这样,您将使用JPanel
的一半大小JFrame
每个{1}}。
此外,重命名您的班级,Panel
属于AWT中班级的名称,Frame
和Label
也是如此,这可能会让您(以及读取您的代码的人)感到困惑。
永远不要扩展JFrame
,而是根据JPanel
构建您的GUI。请参阅extends JFrame vs creating it inside of class和The use of multiple JFrames, Good / Bad practice?普遍的共识认为它很糟糕。
此外,您还应该检查Should I avoid the use of setPreferred|Maximum|MinimumSize() in Swing?再次,是的,您应该改为覆盖getPreferredSize()
方法。
请忘记按照以下方式更改main()
方法,将您的计划放在Event Dispatch Thread (EDT)上:
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
});
}
现在,根据以上所有建议,您的代码现在应如下所示:
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);
}
}
你的输出将是这样的:
请注意,我添加了一些彩色边框,以显示窗格的开始和结束位置以及另一个窗格的开始和结束位置