我一直在尝试使用一些JPanel
和按钮进行简单的JLabel
布局,并在其下方显示一个图形。
我使用扩展JComponent
并在其中加paintComponent
的类创建了图形,然后将该类实例化并添加到第一个面板之后的帧中。只显示图形。
我试过间隔图形,不会改变任何东西。我知道按钮有效,因为当我注释掉添加Graphics对象组件时,它只显示按钮。我该怎么办?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JColorChooser;
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class Frame
{
public static void main(String[] args)
{
JFrame yipee = new JFrame();
final JLabel title = new JLabel("Create your Character");
title.setFont(new Font("Serif", Font.PLAIN, 40));
final JLabel chooseCol = new JLabel("Choose Color");
final JLabel chooseGen = new JLabel("Choose Gender");
final JLabel ranWeap = new JLabel("Random Weapon");
color = (Color.WHITE);
isMale = true;
ranWeapTriggered = false;
JButton ranWeapBut = new JButton("Fancy");
JRadioButton male = new JRadioButton("Male");
male.setSelected(true);
JRadioButton female = new JRadioButton("Female");
ButtonGroup genBut = new ButtonGroup();
genBut.add(male);
genBut.add(female);
JButton pickCol = new JButton("Click Me");
KnightComponent kc = new KnightComponent();
//call method if random weapon pressed
if(ranWeapTriggered)
{
//character.trigger()
ranWeapTriggered = false;
}
JPanel options = new JPanel();
options.add(title);
options.add(chooseCol);
options.add(new JSeparator(SwingConstants.VERTICAL));
options.add(chooseGen);
options.add(new JSeparator(SwingConstants.VERTICAL));
options.add(ranWeap);
options.add(pickCol);
options.add(male);
options.add(female);
options.add(ranWeapBut);
//options.add(dis);
yipee.add(options);
yipee.add(kc);
class PickColorListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
color = JColorChooser.showDialog(null, "Pick Color", color);
if(color == null)
color = color.WHITE;
}
}
class ChooseGenderListener implements ActionListener
{
public ChooseGenderListener(String gIn)
{
gender = gIn;
}
public void actionPerformed(ActionEvent e)
{
if(gender.equals("male"))
isMale = true;
else
isMale = false;
}
private String gender;
}
class RandomWeaponListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ranWeapTriggered = true;
}
}
ActionListener l1 = new PickColorListener();
pickCol.addActionListener(l1);
ActionListener l2 = new ChooseGenderListener("male");
male.addActionListener(l2);
ActionListener l3 = new ChooseGenderListener("female");
female.addActionListener(l3);
yipee.setSize(FRAME_WIDTH, FRAME_HEIGHT);
yipee.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
yipee.setVisible(true);
yipee.setResizable(false);
}
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 600;
private static Color color;
private static boolean isMale;
private static boolean ranWeapTriggered;
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import javax.swing.JComponent;
public class KnightComponent extends JComponent
{
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Knight k = new Knight(0,200);
k.draw(g2);
}
}
答案 0 :(得分:2)
我没有使用BorderLayout
实际上,您是JFrame
默认情况下使用BorderLayout
,有关更多细节,请参阅Laying Out Components Within a Container。
直接的解决方案可能是做某些事情......
yipee.add(options, BorderLayout.NORTH);
yipee.add(kc);
但是因为KnightComponent
没有为布局管理器提供任何大小调整提示,所以它会自动调整为0x0
。您应该覆盖它的getPreferredSize
方法并返回适当的默认大小
你还打破了油漆链,这会导致一些严重的问题,请参阅Painting in AWT and Swing和Performing Custom Painting