- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
frame.setResizable(false);
在我的 TestCalculator 类中,我的 Calculator 类的 JFrame 无法调整大小(不是一个字),但它在框架内的底部和右侧添加了一些像素......或者从一两个面板中删除了一些像素。我不知 Prop 体如何表达,但是我想让框架具有固定大小,没有额外的像素和垃圾。
是的,我确实在另一个网站上询问过,但他们无法帮助我,这将在明天早上到期,我已经为此工作了大约两周,昨天才使计算器工作。我查了一天一夜,没有找到具体的解决办法。
我不确定到底是什么导致它无法正常工作(废话),但它可能在这段代码中的任何地方,也许是它的顺序,我不知道。
计算器类别:
public class SimpleArithmeticCalculator extends JPanel implements ActionListener {
private static final int BTN_ONE = 0;
private static final int BTN_TWO = 1;
private static final int BTN_THREE = 2;
private static final int BTN_DIV = 3;
private static final int BTN_FOUR = 4;
private static final int BTN_FIVE = 5;
private static final int BTN_SIX = 6;
private static final int BTN_MULT = 7;
private static final int BTN_SEVEN = 8;
private static final int BTN_EIGHT = 9;
private static final int BTN_NINE = 10;
private static final int BTN_MINUS = 11;
private static final int BTN_ZERO = 12;
private static final int BTN_DECIMAL = 13;
private static final int BTN_POWER = 14;
private static final int BTN_PLUS = 15;
private static final int BTN_CLEAR = 16;
private static final int BTN_PERCENT = 17;
private static final int BTN_BACKSPACE = 18;
private static final int BTN_CALC = 19;
private static final int NUM_BUTTONS = 20;
private static final int OP_MULT = 0;
private static final int OP_DIV = 1;
private static final int OP_PLUS = 2;
private static final int OP_MINUS = 3;
private static final int OP_POWER = 4;
private static final int OP_PERCENT = 5;
private static char[] buttonTexts = {'7' , '8' , '9' , '/' , '4' ,
'5' , '6' , '*' , '1' , '2' ,
'3' , '-' , '0' , '.' , '^' ,
'+' , 'C' , '%' , '<' , '='};
private static final int[] buttonKeys = {
KeyEvent.VK_NUMPAD7 , KeyEvent.VK_NUMPAD8 , KeyEvent.VK_NUMPAD9 , KeyEvent.VK_DIVIDE , KeyEvent.VK_NUMPAD4 ,
KeyEvent.VK_NUMPAD5 , KeyEvent.VK_NUMPAD6 , KeyEvent.VK_MULTIPLY , KeyEvent.VK_NUMPAD1 , KeyEvent.VK_NUMPAD2 ,
KeyEvent.VK_NUMPAD3 , KeyEvent.VK_MINUS , KeyEvent.VK_NUMPAD0 , KeyEvent.VK_STOP, '^',
KeyEvent.VK_PLUS, KeyEvent.VK_C , '%' , KeyEvent.VK_BACK_SPACE , KeyEvent.VK_ENTER
};
private Font font;
private JButton[] buttons;
private JTextField displayText;
public JPanel calcPanel;
private JPanel textPanel;
private JPanel buttonPanel;
private String buffer;
private int op;
private int opStored;
private double lhs;
private double rhs;
private double rhsStored;
private boolean left;
private boolean clear;
private char lastAction;
public SimpleArithmeticCalculator() {
super();
buffer = new String("0.0");
op = -1;
opStored = -1;
lhs = 0.0;
rhs = 0.0;
rhsStored = 0.0;
left = true;
clear = true;
lastAction = '=';
font = Font.decode("Courier PLAIN 24");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
}
calcPanel = new JPanel();
textPanel = new JPanel();
buttonPanel = new JPanel();
buttonPanel.setForeground(null);
textPanel.setForeground(null);
calcPanel.setForeground(null);
textPanel.setLayout(new GridLayout(1,1,0,0));
buttonPanel.setLayout(new GridLayout(5 , 10 , 10 , 10));
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(10,10,10,10));
textPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
calcPanel.setLayout(new BorderLayout());
calcPanel.add(textPanel , BorderLayout.NORTH);
calcPanel.add(buttonPanel , BorderLayout.CENTER);
add(calcPanel);
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(false);
textPanel.setVisible(true);
displayText.setBackground(textbg);
displayText.setForeground(textcolor);
for (int i = 0 ; i < NUM_BUTTONS ; ++i) {
buttons[i].setForeground(textcolor);
}
}
public void actionPerformed(ActionEvent ev) {
String action = ev.getActionCommand();
char c = action.charAt(0);
boolean calc = false;
if (c == buttonTexts[BTN_CLEAR]) {
buffer = "";
left = true;
}
else if (c == buttonTexts[BTN_BACKSPACE]) {
if (buffer.length() > 0) {
buffer = buffer.substring(0 , buffer.length() - 1);
}
if (lastAction == buttonTexts[BTN_CALC]) {
left = true;
}
clear = false;
}
else if ((c == buttonTexts[BTN_CALC]) || isOpCharacter(c)) {
int opCurrent = op;
if (c == buttonTexts[BTN_CALC]) {
if (!left) {
calc = true;
if (isOpCharacter(lastAction)) {
rhs = GetBufferValue();
rhsStored = rhs;
opCurrent = op;
opStored = op;
}
else if (lastAction == buttonTexts[BTN_CALC]) {
rhs = rhsStored;
opCurrent = opStored;
}
else {
rhs = GetBufferValue();
rhsStored = rhs;
opCurrent = op;
opStored = op;
}
}
}
else if (isOpCharacter(c)) {
opStored = op;
if (c == buttonTexts[BTN_MULT]) {op = OP_MULT;}
if (c == buttonTexts[BTN_DIV]) {op = OP_DIV;}
if (c == buttonTexts[BTN_PLUS]) {op = OP_PLUS;}
if (c == buttonTexts[BTN_MINUS]) {op = OP_MINUS;}
if (c == buttonTexts[BTN_POWER]) {op = OP_POWER;}
if (c == buttonTexts[BTN_PERCENT]) {op = OP_PERCENT;}
if (lastAction == buttonTexts[BTN_CALC]) {
lhs = GetBufferValue();
clear = true;
}
else if (isOpCharacter(lastAction)) {
}
else {
if (left) {
lhs = GetBufferValue();
left = false;
}
else {
calc = true;
rhs = GetBufferValue();
opCurrent = opStored;
opStored = op;
}
clear = true;
}
}
if (calc) {
double result = 0.0;
if (opCurrent == OP_MULT) {result = lhs * rhs;}
if (opCurrent == OP_DIV) {result = lhs / rhs;}
if (opCurrent == OP_PLUS) {result = lhs + rhs;}
if (opCurrent == OP_MINUS) {result = lhs - rhs;}
if (opCurrent == OP_POWER) {result = Math.pow(lhs,rhs);}
if (opCurrent == OP_PERCENT) {result = lhs / 100;}
lhs = result;
buffer = "" + result;
clear = true;
}
}
else {
if (lastAction == buttonTexts[BTN_CALC]) {
left = true;
}
if (clear) {
buffer = "";
clear = false;
}
if (c == buttonTexts[BTN_DECIMAL]) {
if (buffer.indexOf('.') == -1) {
if (buffer.length() == 0 || (buffer.length() == 1 && buffer.charAt(0) == '-')) {
buffer += "0";
}
buffer += ".";
}
}
else if (c == buttonTexts[BTN_ZERO]) {
if (buffer.length() == 0 || (buffer.length() == 1 && buffer.charAt(0) == '-')) {
buffer += "0.";
}
else if (buffer.length() > 0 && (isNonZeroNumber(buffer.charAt(0)) ||
(buffer.charAt(0) == '-' && isNonZeroNumber(buffer.charAt(1))))) {
buffer += "0";
}
else if (buffer.compareTo("0") == 0 || buffer.compareTo("-0") == 0) {
}
else {
buffer += "0";
}
}
else {
if (!buffer.equals("0") && !buffer.equals("-0")) {
buffer += c;
}
}
}
lastAction = c;
displayText.setText(buffer);
}
public double GetBufferValue() {
double d = 0.0;
try {
d = Double.parseDouble(buffer);
}
catch (NumberFormatException e) {
System.out.println("NumberFormatException in GetBufferValue()");
}
return d;
}
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) {
}
}
测试类:
public class TestCalculator {
public static void main(String[] args) {
ImagePanel panel = new ImagePanel(new ImageIcon("01_Crane_AGweb.jpg").getImage());
SimpleArithmeticCalculator calc = new SimpleArithmeticCalculator();
JFrame frame = new JFrame("Simple Arithmetic Calculator");
frame.setResizable(false);
frame.getContentPane().add(panel);
frame.getContentPane().add(calc);
**frame.setPreferredSize(new Dimension(343,343));**
frame.pack();
frame.setVisible(true);
**frame.setMinimumSize(new Dimension(343,371));**
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
panel.setVisible(true);
panel.setOpaque(true);
panel.add(calc);
calc.SetColors(null , Color.white , new Color(72,61,139));
calc.setVisible(true);
calc.setOpaque(false);
}
}
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);
}
}
最佳答案
框架有边框。如果做得正确,pack
将尝试创建一个可视区域(框架边框内的空间)以匹配内容 Pane 的首选大小。
不用担心框架的大小,而是让两个组件和布局管理器自己决定......
第一个值是图像面板/图像的大小,第二个值是窗口的大小。
确保您始终在调用 pack
之前调用 setResizing
...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Window;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestCalculator {
public static void main(String[] args) {
ImagePanel panel = new ImagePanel(new ImageIcon("/path/to/image").getImage());
JFrame frame = new JFrame("Simple Arithmetic Calculator");
frame.getContentPane().add(panel);
frame.setResizable(false);
frame.pack();
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));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
g.setColor(Color.WHITE);
FontMetrics fm = g.getFontMetrics();
Window window = SwingUtilities.getWindowAncestor(this);
int y = fm.getAscent();
g.drawString(getWidth() + "x" + getHeight() + "; " + window.getWidth() + "x" + window.getHeight(), 0, y);
}
}
使用简单的布局示例进行更新
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Window;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class TestCalculator {
public static void main(String[] args) {
new TestCalculator();
}
public TestCalculator() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
ImagePanel panel = new ImagePanel(new ImageIcon("/path/to/image").getImage());
panel.add(new Calculator());
JFrame frame = new JFrame("Simple Arithmetic Calculator");
frame.getContentPane().add(panel);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
public class Calculator extends JPanel {
public Calculator() {
setOpaque(false);
setLayout(new BorderLayout());
add(new JTextField(), BorderLayout.NORTH);
JPanel keysPane = new JPanel(new GridLayout(5, 4, 8, 8));
keysPane.setBorder(new EmptyBorder(4, 0, 0, 4));
keysPane.setOpaque(false);
String keys[] = new String[]{
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "^", "+",
"C", "%", "<", "=",
};
for (String key : keys) {
keysPane.add(new JButton(key));
}
add(keysPane);
}
}
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));
setLayout(new BorderLayout());
setBorder(new EmptyBorder(16, 16, 16, 16));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
g.setColor(Color.WHITE);
FontMetrics fm = g.getFontMetrics();
Window window = SwingUtilities.getWindowAncestor(this);
int y = fm.getAscent();
g.drawString(getWidth() + "x" + getHeight() + "; " + window.getWidth() + "x" + window.getHeight(), 0, y);
}
}
}
从扩展示例更新
做事的顺序非常重要。一般来说,您想要初始化界面,添加所有需要的组件,打包框架并使其可见......
这通常不是您正在做的事情。而不是做类似的事情......
ImagePanel panel = new ImagePanel(new ImageIcon("01_Crane_AGweb.jpg").getImage());
SimpleArithmeticCalculator calc = new SimpleArithmeticCalculator();
JFrame frame = new JFrame("Simple Arithmetic Calculator");
frame.setResizable(false);
// This is not going to come out well, as BorderLayout
// will only allow a single component to occupy the CenterPosition
frame.getContentPane().add(panel);
frame.getContentPane().add(calc);
frame.setPreferredSize(new Dimension(343,343));
// Now sized to the preferred size of the calc pane...
frame.pack();
frame.setVisible(true);
frame.setMinimumSize(new Dimension(343,371));
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// Rather pointless as Swing components are visible by default...
panel.setVisible(true);
panel.setOpaque(true);
// This is now going to change the preferred size of the window...
panel.add(calc);
你可以做一些更像......的事情
ImagePanel panel = new ImagePanel(new ImageIcon("01_Crane_AGweb.jpg").getImage());
SimpleArithmeticCalculator calc = new SimpleArithmeticCalculator();
JFrame frame = new JFrame("Simple Arithmetic Calculator");
panel.setVisible(true);
panel.setOpaque(true);
panel.add(calc);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
相反...
完整运行示例...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestCalculator100 {
public static void main(String[] args) {
new TestCalculator100();
}
public TestCalculator100() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
ImagePanel panel = new ImagePanel(new ImageIcon("01_Crane_AGweb.jpg").getImage());
SimpleArithmeticCalculator calc = new SimpleArithmeticCalculator();
calc.setOpaque(false);
panel.add(calc);
JFrame frame = new JFrame("Simple Arithmetic Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
});
}
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));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
public static class SimpleArithmeticCalculator extends JPanel implements ActionListener {
private static final int BTN_ONE = 0;
private static final int BTN_TWO = 1;
private static final int BTN_THREE = 2;
private static final int BTN_DIV = 3;
private static final int BTN_FOUR = 4;
private static final int BTN_FIVE = 5;
private static final int BTN_SIX = 6;
private static final int BTN_MULT = 7;
private static final int BTN_SEVEN = 8;
private static final int BTN_EIGHT = 9;
private static final int BTN_NINE = 10;
private static final int BTN_MINUS = 11;
private static final int BTN_ZERO = 12;
private static final int BTN_DECIMAL = 13;
private static final int BTN_POWER = 14;
private static final int BTN_PLUS = 15;
private static final int BTN_CLEAR = 16;
private static final int BTN_PERCENT = 17;
private static final int BTN_BACKSPACE = 18;
private static final int BTN_CALC = 19;
private static final int NUM_BUTTONS = 20;
private static final int OP_MULT = 0;
private static final int OP_DIV = 1;
private static final int OP_PLUS = 2;
private static final int OP_MINUS = 3;
private static final int OP_POWER = 4;
private static final int OP_PERCENT = 5;
private static char[] buttonTexts = {'7', '8', '9', '/', '4',
'5', '6', '*', '1', '2',
'3', '-', '0', '.', '^',
'+', 'C', '%', '<', '='};
private static final int[] buttonKeys = {
KeyEvent.VK_NUMPAD7, KeyEvent.VK_NUMPAD8, KeyEvent.VK_NUMPAD9, KeyEvent.VK_DIVIDE, KeyEvent.VK_NUMPAD4,
KeyEvent.VK_NUMPAD5, KeyEvent.VK_NUMPAD6, KeyEvent.VK_MULTIPLY, KeyEvent.VK_NUMPAD1, KeyEvent.VK_NUMPAD2,
KeyEvent.VK_NUMPAD3, KeyEvent.VK_MINUS, KeyEvent.VK_NUMPAD0, KeyEvent.VK_STOP, '^',
KeyEvent.VK_PLUS, KeyEvent.VK_C, '%', KeyEvent.VK_BACK_SPACE, KeyEvent.VK_ENTER
};
private Font font;
private JButton[] buttons;
private JTextField displayText;
public JPanel calcPanel;
private JPanel textPanel;
private JPanel buttonPanel;
private String buffer;
private int op;
private int opStored;
private double lhs;
private double rhs;
private double rhsStored;
private boolean left;
private boolean clear;
private char lastAction;
public SimpleArithmeticCalculator() {
super();
buffer = new String("0.0");
op = -1;
opStored = -1;
lhs = 0.0;
rhs = 0.0;
rhsStored = 0.0;
left = true;
clear = true;
lastAction = '=';
font = Font.decode("Courier PLAIN 24");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
calcPanel = new JPanel();
calcPanel.setOpaque(false);
textPanel = new JPanel();
textPanel.setOpaque(false);
buttonPanel = new JPanel();
buttonPanel.setOpaque(false);
textPanel.setLayout(new GridLayout(1, 1, 0, 0));
buttonPanel.setLayout(new GridLayout(5, 10, 10, 10));
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(10, 10, 10, 10));
textPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
calcPanel.setLayout(new BorderLayout());
calcPanel.add(textPanel, BorderLayout.NORTH);
calcPanel.add(buttonPanel, BorderLayout.CENTER);
add(calcPanel);
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(false);
textPanel.setVisible(true);
displayText.setBackground(textbg);
displayText.setForeground(textcolor);
for (int i = 0; i < NUM_BUTTONS; ++i) {
buttons[i].setForeground(textcolor);
}
}
public void actionPerformed(ActionEvent ev) {
String action = ev.getActionCommand();
char c = action.charAt(0);
boolean calc = false;
if (c == buttonTexts[BTN_CLEAR]) {
buffer = "";
left = true;
} else if (c == buttonTexts[BTN_BACKSPACE]) {
if (buffer.length() > 0) {
buffer = buffer.substring(0, buffer.length() - 1);
}
if (lastAction == buttonTexts[BTN_CALC]) {
left = true;
}
clear = false;
} else if ((c == buttonTexts[BTN_CALC]) || isOpCharacter(c)) {
int opCurrent = op;
if (c == buttonTexts[BTN_CALC]) {
if (!left) {
calc = true;
if (isOpCharacter(lastAction)) {
rhs = GetBufferValue();
rhsStored = rhs;
opCurrent = op;
opStored = op;
} else if (lastAction == buttonTexts[BTN_CALC]) {
rhs = rhsStored;
opCurrent = opStored;
} else {
rhs = GetBufferValue();
rhsStored = rhs;
opCurrent = op;
opStored = op;
}
}
} else if (isOpCharacter(c)) {
opStored = op;
if (c == buttonTexts[BTN_MULT]) {
op = OP_MULT;
}
if (c == buttonTexts[BTN_DIV]) {
op = OP_DIV;
}
if (c == buttonTexts[BTN_PLUS]) {
op = OP_PLUS;
}
if (c == buttonTexts[BTN_MINUS]) {
op = OP_MINUS;
}
if (c == buttonTexts[BTN_POWER]) {
op = OP_POWER;
}
if (c == buttonTexts[BTN_PERCENT]) {
op = OP_PERCENT;
}
if (lastAction == buttonTexts[BTN_CALC]) {
lhs = GetBufferValue();
clear = true;
} else if (isOpCharacter(lastAction)) {
} else {
if (left) {
lhs = GetBufferValue();
left = false;
} else {
calc = true;
rhs = GetBufferValue();
opCurrent = opStored;
opStored = op;
}
clear = true;
}
}
if (calc) {
double result = 0.0;
if (opCurrent == OP_MULT) {
result = lhs * rhs;
}
if (opCurrent == OP_DIV) {
result = lhs / rhs;
}
if (opCurrent == OP_PLUS) {
result = lhs + rhs;
}
if (opCurrent == OP_MINUS) {
result = lhs - rhs;
}
if (opCurrent == OP_POWER) {
result = Math.pow(lhs, rhs);
}
if (opCurrent == OP_PERCENT) {
result = lhs / 100;
}
lhs = result;
buffer = "" + result;
clear = true;
}
} else {
if (lastAction == buttonTexts[BTN_CALC]) {
left = true;
}
if (clear) {
buffer = "";
clear = false;
}
if (c == buttonTexts[BTN_DECIMAL]) {
if (buffer.indexOf('.') == -1) {
if (buffer.length() == 0 || (buffer.length() == 1 && buffer.charAt(0) == '-')) {
buffer += "0";
}
buffer += ".";
}
} else if (c == buttonTexts[BTN_ZERO]) {
if (buffer.length() == 0 || (buffer.length() == 1 && buffer.charAt(0) == '-')) {
buffer += "0.";
} else if (buffer.length() > 0 && (isNonZeroNumber(buffer.charAt(0))
|| (buffer.charAt(0) == '-' && isNonZeroNumber(buffer.charAt(1))))) {
buffer += "0";
} else if (buffer.compareTo("0") == 0 || buffer.compareTo("-0") == 0) {
} else {
buffer += "0";
}
} else {
if (!buffer.equals("0") && !buffer.equals("-0")) {
buffer += c;
}
}
}
lastAction = c;
displayText.setText(buffer);
}
public double GetBufferValue() {
double d = 0.0;
try {
d = Double.parseDouble(buffer);
} catch (NumberFormatException e) {
System.out.println("NumberFormatException in GetBufferValue()");
}
return d;
}
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]);
}
}
}
关于java - frame.setResizable(false) 不起作用!它改变窗口大小而不是保持尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20463519/
我有以下MWE function f(p) ans = zeros(p, 2) return ans end ans = f(2) ans b=ans.+1.0 ans 起初,ans是正确的,
OWIN AppBuilder“UseStatic”位从本地文件系统传送文件,这在某些情况下很方便,但我希望它从我在应用程序启动时预先填充的内存中 IDictionary 传送内容。任何人都可以指出一
我是 JavaScript 新手。 我的代码允许我列出 JSON 文档的元素及其类型,并将所有元素连接到一个字符串 donnees_types 中。 问题是 JavaScript 中的 typeof
我想在每次刷新时更改主页上的背景图像。我怎样才能做到这一点?我认为 jquery 是可能的,但我不太清楚。 感谢您对此主题的任何帮助或评论。 最佳答案 我不知道“如何”,但我找到了以下链接: http
所以我已经在这上面花了几个小时了,老实说我完全陷入困境。我写了一个 for 循环来计算整数中的数字数量,但我发现一旦我输入 10 位以上的数字,除数值就会发生变化,而且我不明白为什么。我在互联网上搜索
当我在使用表面 View 的游戏 Activity 和使用膨胀菜单的其他 Activity 之间切换时,我会收到错误消息。 日志猫: 07-13 15:15:34.464: ERROR/Android
听说很简单 R*=f; G*=f; B*=f; 其中 f 是标量值 0 .. 1.0 或更大改变亮度的方法不太正确颜色,但我找不到一些代码片段获得更好的东西(无需太多学习色彩理论)也许有人可以在这里给
如以下链接所述:- How to get the ThreadPoolExecutor to increase threads to max before queueing? 我将队列实现更改为在进入
我只显示最初提供 20 分钟 slotMinutes 的日历。我试图让用户即时更改为 10 分钟的 slotMinutes。 我有一个触发以下代码的按钮: $('#calendar').fullCal
我的问题是:我的应用程序中有一个新闻列表(UITableView)当我点击 1 个"new"时,我打开它,里面有一个后退按钮,可以让我回到列表。现在的问题是我必须在滑动时实现"new"更改,所以我制作
我面临着与 I'm trying to install psycopg2 onto Mac OS 10.6.3; it claims it can't find "stdarg.h" but I ca
需要通过为 array2 中不存在的索引设置 visible false 来从 array1 创建一个新的 array3。 在下面的示例中,我有索引 0,2。所以对于 1,3,结果数组必须具有 vis
我有一个对象,类似这样 var Egg = function(){ this.test = $(.slider .label); $('.slider').slider({
我想改变 ScrollView 的宽度。首先,我这样做了: var scrollWidthConstraint: NSLayoutConstraint! 然后设置它: scrollWidthConst
我有两个动画,一个是“过渡”,它在悬停时缩小图像,另一个是 animation2,其中图像的不透明度以周期性间隔重复变化。 我有 animation2 在图像上进行,当我将鼠标悬停在它上面时,anim
我是一个 jQuery 新手,一直在尝试添加一个脚本来更改 div onClick 的 id。 这是一个jsfiddle example . $(function accept() { $("
我正在尝试更改用户输入的字母的大小写,并将该字母的小写和大写版本存储在变量中。我已经编写了下面的代码,但它在运行时遇到了问题。有人指出是什么导致了问题吗? #include #include #i
假设我有这段代码: char num[2] = {15, 240}; char* p_num; 现在,如果我理解的一切正确,数组中的位应该像这样对齐: 00001111 11110000 我的问题是
var html = '' + count + '' + i.Description + '' + i.Priority + '' + i.Status + 'johnsmith- ' + creat
我在虚拟机上安装了 minix3,并希望我可以操纵当前的队列选择算法,以便我可以将其从优先级顺序更改为包括随机分类的低优先级作业的优先级顺序。我发现我需要更改的代码部分在 proc.c 中,具体部分是
我是一名优秀的程序员,十分优秀!