在JLabel中绘制线条并在JFrame中添加为JPanel不起作用 [英] Draw Line in a JLabel and add as JPanel in a JFrame does not function
问题描述
我在Java中非常新,因此对任何愚蠢的问题感到抱歉.
im very new in Java so sorry for any stupid question.
这是我的问题:
我只想画一条线-如果将JLabel
Myline()
直接作为label
添加到JFrame MyFrame
,则它将显示在生成的JFrame
-Dialogwindow中,但是如果添加MyLine()
到JPanel
panel
和panel
到MyFrame
,则没有行.
I just want to draw a line - if I add the JLabel
Myline()
directly as label
to the JFrame MyFrame
, then it will be showing in the resulting JFrame
-Dialogwindow, but if I add MyLine()
to the JPanel
panel
and panel
to MyFrame
then there is no Line.
为什么?
package examples;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class Bsp {
public static void main(String[] args) {
JFrame frame = new MyFrame();
}
}
class MyFrame extends JFrame {
private class MyLine extends JLabel {
int width;
public MyLine(int width) {
this.width=width;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(0, 0, getWidth(), 0);
g.dispose();
}
}
public MyFrame() {
JLabel label=new MyLine(getWidth());
JPanel panel=new JPanel(new GridBagLayout());
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 1));
panel.add(label);
add(new JButton("A"));
add(new JButton("B"));
add(panel);
pack();
setVisible(true);
}
}
推荐答案
MyFrame
没有首选大小,直到pack()
询问封闭组件的首选大小.设置MyLine
首选大小以查看效果.另请参见 初始线程 .
MyFrame
has no preferred size until pack()
interrogates the preferred sizes of the enclosed components. Give MyLine
a preferred size to see the effect. See also Initial Threads.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Bsp {
public static void main(String[] args) {
JFrame frame = new MyFrame();
}
}
class MyFrame extends JFrame {
private class MyLine extends JLabel {
int width;
public MyLine(int width) {
this.width = width;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(0, 0, getWidth(), getHeight());
g.dispose();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(width, 16);
}
}
public MyFrame() {
JLabel label = new MyLine(32);
System.out.println(getWidth());
JPanel panel = new JPanel(new GridBagLayout());
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 1));
panel.add(label);
add(new JButton("A"));
add(new JButton("B"));
add(panel);
pack();
setVisible(true);
}
}
这篇关于在JLabel中绘制线条并在JFrame中添加为JPanel不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!