程序运行时,按钮被绘制为10X10。 我通过“修改菜单”更改了按钮20X20。 但是,该按钮不可见。
将鼠标移到按钮上时出现。 不要重画。 重新验证也不一样。
package com.test;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MenuTest extends JFrame {
private Dimension dimen, dimen1;
private int xpos, ypos;
private JButton[][] btn = null;
private JPanel p;
private GridLayout grid;
private CardLayout card;
private int rownum;
private int colnum;
private int mineLevel;
public MenuTest() {
super("GAME");
p = new JPanel();
grid = new GridLayout();
card = new CardLayout();
createMenu();
starting();
}
private void createMenu() {
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
JMenu filemenu = new JMenu("파일(F)");
filemenu.setMnemonic('F');
JMenuItem startmenu = new JMenuItem("게임 시작(S)");
startmenu.setMnemonic('S');
startmenu.setActionCommand("START");
startmenu.addActionListener(new MenuActionListener());
filemenu.add(startmenu);
JMenuItem minecntmenu = new JMenuItem("변경(M)");
minecntmenu.setMnemonic('M');
minecntmenu.setActionCommand("MODIFY");
minecntmenu.addActionListener(new MenuActionListener());
filemenu.add(minecntmenu);
JMenuItem close = new JMenuItem("닫기(C)");
close.setMnemonic('C');
filemenu.add(close);
bar.add(filemenu); //JMenuBar에 JMenu 부착
//도움말 메뉴 만들기--------------------------------
JMenu helpmenu = new JMenu("도움말(D)");
helpmenu.setMnemonic('D'); //단축키를 Alf + D 로 설정
JMenuItem help = new JMenuItem("Help(H)");
help.setMnemonic('H');
helpmenu.add(help);
bar.add(helpmenu);
}
private class MenuActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("START")) {
JOptionPane.showMessageDialog(null, "게임시작", "게임", JOptionPane.YES_NO_OPTION);
} else if (e.getActionCommand().equals("MODIFY")) {
modify();
JOptionPane.showMessageDialog(null, "변경", "게임", JOptionPane.YES_NO_OPTION);
}
}
}
private void modify() {
btn = null;
setRowColnum(20, 20);
MapInit(20);
LayoutSet(400, 500);
}
private void starting() {
setRowColnum(10, 10);
MapInit(10);
LayoutSet(200, 250);
}
private void setRowColnum(int rownum, int colnum) {
this.rownum = rownum;
this.colnum = colnum;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MenuTest mt = new MenuTest();
}
public void LayoutSet(int w, int h) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(w, h);
dimen = Toolkit.getDefaultToolkit().getScreenSize();
dimen1 = this.getSize();
xpos = (int) (dimen.getWidth() / 2 - dimen1.getWidth() / 2);
ypos = (int) (dimen.getHeight() / 2 - dimen1.getHeight() / 2);
this.setLocation(xpos, ypos);
this.setVisible(true);
this.setResizable(false);
}
public void MapInit(int minecnt) {
p.removeAll();
setBtn(rownum, colnum);
card = new CardLayout(5, 5);
this.setLayout(card);
grid = new GridLayout(rownum, colnum, 0, 0);
p = new JPanel(grid);
int action_num = 0;
for (int i = 0; i < btn.length; i++) {
for (int j = 0; j < btn[i].length; j++) {
action_num = (i * 10 + j);
btn[i][j] = new JButton();
p.add(btn[i][j]);
}
}
this.repaint();
this.add("View", p);
}
private void setBtn(int row, int col) {
btn = new JButton[row][col];
}
}
答案 0 :(得分:2)
在可见GUI中添加/删除组件时,基本代码为:
panel.remove(...);
panel.add(...);
...
panel.revalidate();
panel.reapint();
删除/添加所有组件后,您需要执行revalidate()
,以便调用布局管理器,并为所有组件指定大小和位置。然后repaint()
确保组件被绘制。