我目前正在编写计算器。我有一个JFrame,JPanel上有各种各样的元素(JButtons,TextArea)。但是,当程序首次开始运行时,没有任何按钮可见,直到鼠标悬停在它上面(菜单栏和文本区域可见)。此外,每当任何元素被“覆盖”时,无论是通过放置菜单还是将鼠标移出屏幕然后重新打开它,元素的那部分现在变成灰色。
我正在四处阅读,并且能够找到一些人提到这个问题,使用repaint();解决它,但这对我来说似乎不起作用。
这是我到目前为止的代码:
BasicCalculatorFrame.java
public class BasicCalculatorFrame extends JFrame{
BasicCalculatorPanel calcPanel = new BasicCalculatorPanel();
BasicCalculatorFrame(){
//setSize(219, 314);
setSize(279, 374);
setResizable(false);
setTitle("Calculator");
add(calcPanel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setJMenuBar(calcPanel.menuBar);
add(calcPanel.mainPanel);
revalidate();
repaint();
}
}
BasicCalculatorPanel.java
(我知道现在不太精简,正在努力让事情先发挥作用)
public class BasicCalculatorPanel extends JPanel {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;
JCheckBoxMenuItem cbMenuItem;
JPanel mainPanel;
JTextField displayScreen;
String onClipBoard = null, memory = null, temp;
JButton buttonMC, buttonMR, buttonMS, buttonMPlus, buttonMMinus, buttonBack, buttonCE, buttonC, buttonPlusMinus, buttonSqrt, button7, button8, button9, buttonDiv, buttonMod, button4, button5, button6, buttonTimes, button1OverX, button1, button2, button3, buttonMinus, button0, buttonPoint, buttonPlus, buttonEquals;
Font displayFont = new Font(Font.SANS_SERIF, Font.BOLD, 22), buttonFont = new Font(Font.DIALOG, Font.PLAIN, 10);
Clipboard board;
double lastVariable = 0;
Double test = null;
ActionListener notImplementedListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
notRequiredMessage();
}
};
public BasicCalculatorPanel() {
menuBar = new JMenuBar();
//Create the view menu option.
createViewMenu();
//Create the edit menu.
createEditMenu();
//Create the help menu.
createHelpMenu();
//Create the panel.
createCalculatorPanel();
revalidate();
repaint();
}
private void createViewMenu() {
//View menu and it's items.
menu = new JMenu("View");
menu.setMnemonic(KeyEvent.VK_V);
menuBar.add(menu);
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("Standard");
rbMenuItem.setSelected(true);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Scientific");
rbMenuItem.addActionListener(notImplementedListener);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Programmer");
rbMenuItem.addActionListener(notImplementedListener);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Statistics");
rbMenuItem.addActionListener(notImplementedListener);
group.add(rbMenuItem);
menu.add(rbMenuItem);
menu.addSeparator();
cbMenuItem = new JCheckBoxMenuItem("History");
cbMenuItem.addActionListener(notImplementedListener);
menu.add(cbMenuItem);
cbMenuItem = new JCheckBoxMenuItem("Digit grouping");
cbMenuItem.addActionListener(notImplementedListener);
menu.add(cbMenuItem);
menu.addSeparator();
group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("Basic");
rbMenuItem.setSelected(true);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Unit conversion");
rbMenuItem.addActionListener(notImplementedListener);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Date calculation");
rbMenuItem.addActionListener(notImplementedListener);
group.add(rbMenuItem);
menu.add(rbMenuItem);
submenu = new JMenu("Worksheets");
rbMenuItem = new JRadioButtonMenuItem("Mortgage");
rbMenuItem.addActionListener(notImplementedListener);
group.add(rbMenuItem);
submenu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Vehicle lease");
rbMenuItem.addActionListener(notImplementedListener);
group.add(rbMenuItem);
submenu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Fuel economy (mpg)");
rbMenuItem.addActionListener(notImplementedListener);
group.add(rbMenuItem);
submenu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Fuel economy (L/100km)");
rbMenuItem.addActionListener(notImplementedListener);
group.add(rbMenuItem);
submenu.add(rbMenuItem);
menu.add(submenu);
}
private void createEditMenu() {
menu = new JMenu("Edit");
menu.setMnemonic(KeyEvent.VK_E);
menuBar.add(menu);
menuItem = new JMenuItem("Copy");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String myString = displayScreen.getText();
StringSelection stringSelection = new StringSelection(myString);
board = Toolkit.getDefaultToolkit().getSystemClipboard();
board.setContents(stringSelection, null);
}
});
menu.add(menuItem);
menuItem = new JMenuItem("Paste");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
board = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable content = board.getContents(null);
try {
onClipBoard = (String) content.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException ex) {
Logger.getLogger(BasicCalculatorPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(BasicCalculatorPanel.class.getName()).log(Level.SEVERE, null, ex);
}
test = Double.parseDouble(onClipBoard);
if (test != null) {
displayScreen.setText(onClipBoard);
}
}
});
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem("History");
menuItem.addActionListener(notImplementedListener);
menu.add(menuItem);
}
private void notRequiredMessage() {
JOptionPane.showMessageDialog(null, "Hey, I didn't need to implement this!");
}
private void createHelpMenu() {
menu = new JMenu("Help");
menu.setMnemonic(KeyEvent.VK_H);
menuBar.add(menu);
menuItem = new JMenuItem("View help");
menuItem.addActionListener(notImplementedListener);
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem("About calculator");
menuItem.addActionListener(notImplementedListener);
menu.add(menuItem);
}
private void createCalculatorPanel() {
setDisplayMenu();
buttonMC = new JButton("MC");
buttonMC.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
memory = null;
}
});
buttonMC.setText("MC");
buttonMR = new JButton("MR");
buttonMR.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayScreen.setText(memory);
}
});
buttonMS = new JButton("MS");
buttonMS.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
memory = displayScreen.getText();
}
});
buttonMPlus = new JButton("M+");
buttonMPlus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double total = Double.parseDouble(memory);
total += Double.parseDouble(displayScreen.getText());
memory = Double.toString(total);
}
});
buttonMMinus = new JButton("M-");
buttonMMinus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Double total = Double.parseDouble(memory);
total -= Double.parseDouble(displayScreen.getText());
memory = Double.toString(total);
}
});
buttonBack = new JButton("\u2190");
buttonBack.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str = displayScreen.getText();
displayScreen.setText(str.substring(0, str.length() - 1));
}
});
buttonCE = new JButton("CE");
buttonCE.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayScreen.setText("0");
}
});
buttonC = new JButton("C");
buttonC.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayScreen.setText("0");
}
});
buttonPlusMinus = new JButton("\u00B1");
buttonPlusMinus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Double inverted = 0 - Double.parseDouble(displayScreen.getText());
displayScreen.setText(Double.toString(inverted));
}
});
buttonSqrt = new JButton("\u221A");
buttonSqrt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Double root = Math.sqrt(Double.parseDouble(displayScreen.getText()));
displayScreen.setText(Double.toString(root));
}
});
button7 = new JButton("7");
addDigitButtonListener(button7);
button8 = new JButton("8");
addDigitButtonListener(button8);
button9 = new JButton("9");
addDigitButtonListener(button9);
buttonDiv = new JButton("/");
buttonDiv.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//TODO Insert Logic Here
}
});
buttonMod = new JButton("%");
buttonMod.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Even Google couldn't explain how this button works.");
}
});
button4 = new JButton("4");
addDigitButtonListener(button4);
button5 = new JButton("5");
addDigitButtonListener(button5);
button6 = new JButton("6");
addDigitButtonListener(button6);
buttonTimes = new JButton("*");
buttonTimes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//TODO add logic.
}
});
button1OverX = new JButton("1/x");
button1OverX.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double temp = 1 / Double.parseDouble(displayScreen.getText());
displayScreen.setText(Double.toString(temp));
}
});
GridLayout topSetOfButton = new GridLayout(4, 5, 3, 3);
JPanel topButtonsPanel = new JPanel(topSetOfButton);
mainPanel.add(topButtonsPanel, BorderLayout.CENTER);
topButtonsPanel.add(buttonMC);
topButtonsPanel.add(buttonMR);
topButtonsPanel.add(buttonMS);
topButtonsPanel.add(buttonMPlus);
topButtonsPanel.add(buttonMMinus);
topButtonsPanel.add(buttonBack);
topButtonsPanel.add(buttonCE);
topButtonsPanel.add(buttonC);
topButtonsPanel.add(buttonPlusMinus);
topButtonsPanel.add(buttonSqrt);
topButtonsPanel.add(button7);
topButtonsPanel.add(button8);
topButtonsPanel.add(button9);
topButtonsPanel.add(buttonDiv);
topButtonsPanel.add(buttonMod);
topButtonsPanel.add(button4);
topButtonsPanel.add(button5);
topButtonsPanel.add(button6);
topButtonsPanel.add(buttonTimes);
topButtonsPanel.add(button1OverX);
}
private void setDisplayMenu() {
mainPanel = new JPanel(new BorderLayout());
displayScreen = new JTextField();
displayScreen.setEditable(false);
displayScreen.setBackground(Color.white);
displayScreen.setMinimumSize(new Dimension(200, 185));
displayScreen.setFont(displayFont);
displayScreen.setHorizontalAlignment(JTextField.RIGHT);
displayScreen.setText("0");
mainPanel.add(displayScreen, BorderLayout.NORTH);
}
private void addDigitButtonListener(JButton button) {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (displayScreen.getText().equals("0") || displayScreen.getText().equals("NaN") || displayScreen.getText().equals("Infinity"))
{
displayScreen.setText(e.getActionCommand());
}
else {
temp = displayScreen.getText() + e.getActionCommand();
displayScreen.setText(temp);
}
}
});
}
}