java 多个 panel_在Java中,每次我向JPanel添加多个东西时,第二...
我創建了一個JPanel并添加了幾個簡單的按鈕,并附加了監聽器.然后我決定將一個Image添加到我的面板的背景中,所以我將我的JPanel切換到了ImagePanel.按鈕正在JPanel上工作,但現在我為要顯示的背景圖像添加了一堆代碼,按鈕不再顯示.我沒有更改任何按鈕添加代碼,所以我很困惑為什么按鈕不再顯示.這也發生在我單獨的GameFrame類中.我在面板中添加了2個矩形組件,然后是3個按鈕.對于該面板,只有按鈕顯示,盡管在添加按鈕之前矩形工作.我可以每個面板只有一種類型的JComponent嗎?我真的不明白為什么這樣做.感謝您的時間.
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TitleFrame extends JFrame
{
private ImagePanel panel;
private JButton mage;
private JButton rogue;
private JButton warrior;
private Image image;
public TitleFrame()
{
JFrame frame = new JFrame();
frame.setSize(1024, 768);
frame.setTitle("Title Screen");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
createMageButton();
createRogueButton();
createWarriorButton();
ImagePanel panel = new ImagePanel(new ImageIcon("C:/Users/Derek Reitz/Documents/Eclipse Projects/GameApp/src/background.jpg").getImage());
panel.add(mage);
panel.add(rogue);
panel.add(warrior);
panel.paintComponent(frame.getGraphics());
frame.getContentPane().add(panel);
}
private void createRogueButton() {
rogue = new JButton("Select Rogue");
class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
moveToNextFrame('r');
}
}
ActionListener listener = new AddButtonListener();
rogue.addActionListener(listener);
}
private void createWarriorButton() {
warrior = new JButton("Select Warrior");
class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
moveToNextFrame('w');
}
}
ActionListener listener = new AddButtonListener();
warrior.addActionListener(listener);
}
private void createMageButton() {
mage = new JButton("Select Mage");
class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
moveToNextFrame('m');
}
}
ActionListener listener = new AddButtonListener();
mage.addActionListener(listener);
}
public void moveToNextFrame(char c)
{
GameFrame game = new GameFrame(c);
}
class ImagePanel extends JPanel
{
private Image img;
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
}
總結
以上是生活随笔為你收集整理的java 多个 panel_在Java中,每次我向JPanel添加多个东西时,第二...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java comet_用java实现co
- 下一篇: java两个日期之间的日期_获取两个日期