我很着急,所以我想知道重复的事情。我仍在努力学习 Java 和术语,直到本学期结束。我用了一个模板。我正在使用背景图像(“面板”),这使一切变得复杂。
基本上,这些按钮仅当我将鼠标悬停在它们上方时才会显示。显然,它与 JPanel 有关。
我排除了您可能会要求的代码,希望这次有人帮助我,因为我的按钮与我在查看其他推荐帖子时看到的按钮不同。
另外,我可以将 JFrame 设置为固定大小(测试类代码中的大小)吗?
代码可能是多余的,但我只是想让一切正常工作。请记住,我是 Java 新手。
测试类别:
public class TestCalculator {
public static void main(String[] args) {
JFrame frame = new JFrame(TestCalculator.class.getSimpleName());
ImagePanel panel = new ImagePanel(new ImageIcon("01_Crane_AGweb.jpg").getImage());
SimpleArithmeticCalculator calc = new SimpleArithmeticCalculator();
calc.SetColors(null , Color.white , new Color(72,61,139));
calc.setVisible(true);
calc.setOpaque(false);
panel.setVisible(true);
panel.setOpaque(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
calc.add(panel);
frame.add(panel);
frame.add(calc);
frame.getContentPane().add(calc);
frame.setPreferredSize(new Dimension(358,379));
frame.setMinimumSize(new Dimension(358,379));
frame.setVisible(true);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
this.setPreferredSize(new Dimension(size));
this.setMinimumSize(new Dimension(size));
this.setMaximumSize(new Dimension(size));
this.setSize(new Dimension(size));
this.setLayout(null);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this.getWidth(),this.getHeight(),this);
}
}
MAIN:
public class SimpleArithmeticCalculator extends JPanel implements ActionListener {
//BUTTONSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS !
...
// jpanels, buttons, font, values, etc
public SimpleArithmeticCalculator() {
super();
//I THINK this is the problem:
buttonPanel.setForeground(null);
textPanel.setForeground(null);
calcPanel.setForeground(null);
textPanel.setLayout(new GridLayout(0,1,0,0));
buttonPanel.setLayout(new GridLayout(0 , 5 , 5 , 5));
displayText = new JTextField("" , 20);
displayText.setHorizontalAlignment(JTextField.RIGHT);
displayText.setFont(font);
displayText.setEditable(false);
textPanel.add(displayText);
buttons = new JButton[NUM_BUTTONS];
for (int i = 0 ; i < NUM_BUTTONS ; ++i) {
buttons[i] = new JButton("" + buttonTexts[i]);
buttons[i].setMnemonic(buttonKeys[i]);
buttons[i].setFont(font);
buttons[i].setMinimumSize(new Dimension(50,50));
buttons[i].setActionCommand("" + buttonTexts[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
buttons[BTN_POWER].setText("^");
buttons[BTN_PERCENT].setText("%");
buttonPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
textPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
calcPanel.setLayout(new BorderLayout());
calcPanel.add(textPanel , BorderLayout.NORTH);
calcPanel.add(buttonPanel , BorderLayout.CENTER);
add(calcPanel);
setMinimumSize(new Dimension(358,379));
setPreferredSize(new Dimension(359,381));
for (int i = 0 ; i < NUM_BUTTONS ; ++i) {
buttons[i].setMaximumSize(buttons[i].getSize());
}
}
public void SetColors(Color bg , Color textbg , Color textcolor) {
calcPanel.setBackground(bg);
calcPanel.setVisible(true);
calcPanel.setOpaque(false);
buttonPanel.setBackground(bg);
buttonPanel.setVisible(true);
buttonPanel.setOpaque(false);
textPanel.setBackground(bg);
textPanel.setOpaque(true);
textPanel.setVisible(true);
displayText.setBackground(textbg);
displayText.setForeground(textcolor);
for (int i = 0 ; i < NUM_BUTTONS ; ++i) {
buttons[i].setForeground(textcolor);
}
}
//ACTION PERFORMED STUFF & OPERATIONS, BLAH
public boolean isOpCharacter(char c) {
return ((c == buttonTexts[BTN_MULT]) ||
(c == buttonTexts[BTN_DIV]) ||
(c == buttonTexts[BTN_PLUS]) ||
(c == buttonTexts[BTN_MINUS]) ||
(c == buttonTexts[BTN_POWER]) ||
(c == buttonTexts[BTN_PERCENT]));
}
public boolean isNumericCharacter(char c) {
return ((c == buttonTexts[BTN_ZERO]) ||
(c == buttonTexts[BTN_ONE]) ||
(c == buttonTexts[BTN_TWO]) ||
(c == buttonTexts[BTN_THREE]) ||
(c == buttonTexts[BTN_FOUR]) ||
(c == buttonTexts[BTN_FIVE]) ||
(c == buttonTexts[BTN_SIX]) ||
(c == buttonTexts[BTN_SEVEN]) ||
(c == buttonTexts[BTN_EIGHT]) ||
(c == buttonTexts[BTN_NINE]) ||
(c == buttonTexts[BTN_DECIMAL]));
}
public boolean isNonZeroNumber(char c) {
return (c == buttonTexts[BTN_ONE] ||
c == buttonTexts[BTN_TWO] ||
c == buttonTexts[BTN_THREE] ||
c == buttonTexts[BTN_FOUR] ||
c == buttonTexts[BTN_FIVE] ||
c == buttonTexts[BTN_SIX] ||
c == buttonTexts[BTN_SEVEN] ||
c == buttonTexts[BTN_EIGHT] ||
c == buttonTexts[BTN_NINE]);
}
public static void main(String[] args) { }
}