我是Java的新手,我们得到了一项任务。我必须创建一个执行某些操作的数学框架,并以某种方式格式化GUI。除了将三个文本字段堆叠在顶部之外,我可以做任何事情。像这样:
t1
t2
t3
这三个垂直向左倾斜,而其他四个方框横跨底部。
其中t1,t2和t3是文本字段,1,2,3和4是操作的功能。
我不能让x,y和z在左边。这是我的代码,请尽可能帮忙!
ConvertFrame.java:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class ConvertFrame extends JFrame {
private final JLabel prompt; // label to prompt user to enter Fahrenheit
private final JLabel display; // label to display temperature in Celsius
private final JTextField temperatureF; // textfield to enter temperature
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JTextField t1;
private JTextField t2;
private JTextField t3;
private JLabel operation;
private JLabel equL;
// constructor sets up GUI
public ConvertFrame() {
super("Math Frame");
prompt = new JLabel("Enter two numbers:");
operation = new JLabel(" OPR ");
equL = new JLabel(" = ");
temperatureF = new JTextField(10); // textfield
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 3));
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(1, 5));
b1 = new JButton("ADD");
b2 = new JButton("SUB");
b3 = new JButton("MUL");
b4 = new JButton("CLEAR");
t1 = new JTextField(7);
t2 = new JTextField(7);
t3 = new JTextField(7);
panel2.add(t1);
panel2.add(operation);
panel2.add(t2);
panel2.add(equL);
panel2.add(t3);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
// register anonymous action listener
Handler1 h1 = new Handler1();
Handler2 h2 = new Handler2();
Handler3 h3 = new Handler3();
Handler4 h4 = new Handler4();
b1.addActionListener(h1);
b2.addActionListener(h2);
b3.addActionListener(h3);
b4.addActionListener(h4);
display = new JLabel("");
add(prompt, BorderLayout.NORTH); // north region
add(panel2, BorderLayout.CENTER); // center region
add(panel, BorderLayout.SOUTH); // south region
} // end ConvertFrame constructor
private class Handler1 implements ActionListener // anonymous inner class
{
public void actionPerformed(ActionEvent e) {
String st1 = t1.getText();
String st2 = t2.getText();
int x = Integer.parseInt(st1);
int y = Integer.parseInt(st2);
int z = x + y;
String st3 = "";
st3 = st3 + z;
t3.setText(st3);
}
} //
private class Handler2 implements ActionListener // anonymous inner class
{
public void actionPerformed(ActionEvent e) {
String st1 = t1.getText();
String st2 = t2.getText();
int x = Integer.parseInt(st1);
int y = Integer.parseInt(st2);
int z = x - y;
String st3 = "";
st3 = st3 + z;
t3.setText(st3);
}
} // end anonymous inner class
private class Handler3 implements ActionListener // anonymous inner class
{
public void actionPerformed(ActionEvent e) {
String st1 = t1.getText();
String st2 = t2.getText();
int x = Integer.parseInt(st1);
int y = Integer.parseInt(st2);
int z = x * y;
String st3 = "";
st3 = Integer.toString(z);
t3.setText(st3);
}
} // end anonymous inner class
private class Handler4 implements ActionListener // anonymous inner class
{
public void actionPerformed(ActionEvent e) {
t1.setText("");
t2.setText("");
t3.setText("");
}
} // end anonymous inner class
// end call to addActionListener
} // end class ConvertFrame
Convert.java:
import javax.swing.JFrame;
public class Convert {
public static void main(String[] args) {
ConvertFrame convertFrame = new ConvertFrame();
convertFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
convertFrame.setSize(500, 400); // set frame size
convertFrame.setVisible(true); // display frame
}
} // end class
答案 0 :(得分:4)
如果你想在顶部堆叠文本字段
你应该改变这个
panel2.setLayout(new GridLayout(1, 5));
到这个
panel2.setLayout(new GridLayout(5, 1));
因为在new GridLayout(row, column)
中第一个参数是行计数和下一个列计数。目前你有1行和5列。所以文本字段定位为coulmns。如果你想垂直定位那么你应该使用1列和5行
答案 1 :(得分:3)
示例代码:
class Asset_Analysis(Base):
#Tell SQLAlchemy what the table name is and if there's any table-specific arguments it should know about
__tablename__ = 'Asset_Analysis'
__table_args__ = {'sqlite_autoincrement': True}
#tell SQLAlchemy the name of column and its attributes:
id = Column(Integer, primary_key=True, nullable=False)
fid = Column(Integer, ForeignKey('Price_History.id'))
<强>输出:强>
注意:强>
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class ConvertFrame extends JFrame {
// Label to prompt user to enter Fahrenheit
private final JLabel prompt;
// Label to display temperature in Celsius
private final JLabel display;
/* // Textfield to enter temperature
private final JTextField temperatureF;*/
// JButtons
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
// JTextFields
private JTextField t1;
private JTextField t2;
private JTextField t3;
private JLabel operation;
private JLabel equL;
// constructor sets up GUI
public ConvertFrame() {
super("Math Frame");
prompt = new JLabel("Enter two numbers:");
operation = new JLabel("OPR");
equL = new JLabel("=");
b1 = new JButton("ADD");
b2 = new JButton("SUB");
b3 = new JButton("MUL");
b4 = new JButton("CLEAR");
t1 = new JTextField(7);
t2 = new JTextField(7);
t3 = new JTextField(7);
// Using GridBagLayout
JPanel layoutPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 4;
c.anchor = GridBagConstraints.CENTER;
c.insets = new Insets(10, 0, 20, 0);
layoutPanel.add(prompt, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 4;
c.ipadx = 20;
c.anchor = GridBagConstraints.CENTER;
c.insets = new Insets(10, 0, 10, 0);
layoutPanel.add(t1, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 4;
c.anchor = GridBagConstraints.CENTER;
layoutPanel.add(operation, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 4;
c.ipadx = 20;
c.anchor = GridBagConstraints.CENTER;
c.insets = new Insets(10, 0, 10, 0);
layoutPanel.add(t2, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 4;
c.anchor = GridBagConstraints.CENTER;
layoutPanel.add(equL, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 5;
c.gridwidth = 4;
c.ipadx = 20;
c.insets = new Insets(10, 0, 10, 0);
c.anchor = GridBagConstraints.CENTER;
layoutPanel.add(t3, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 6;
c.insets = new Insets(30, 5, 10, 5);
c.anchor = GridBagConstraints.CENTER;
layoutPanel.add(b1, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 6;
c.insets = new Insets(30, 5, 10, 5);
c.anchor = GridBagConstraints.CENTER;
layoutPanel.add(b2, c);
c = new GridBagConstraints();
c.gridx = 2;
c.gridy = 6;
c.insets = new Insets(30, 5, 10, 5);
c.anchor = GridBagConstraints.CENTER;
layoutPanel.add(b3, c);
c = new GridBagConstraints();
c.gridx = 3;
c.gridy = 6;
c.insets = new Insets(30, 5, 10, 5);
c.anchor = GridBagConstraints.CENTER;
layoutPanel.add(b4, c);
Handler1 h1 = new Handler1();
Handler2 h2 = new Handler2();
Handler3 h3 = new Handler3();
Handler4 h4 = new Handler4();
b1.addActionListener(h1);
b2.addActionListener(h2);
b3.addActionListener(h3);
b4.addActionListener(h4);
display = new JLabel("");
add(layoutPanel, BorderLayout.CENTER); // center region
} // end ConvertFrame constructor
private class Handler1 implements ActionListener // anonymous inner class
{
public void actionPerformed(ActionEvent e) {
String st1 = t1.getText();
String st2 = t2.getText();
int x = Integer.parseInt(st1);
int y = Integer.parseInt(st2);
int z = x + y;
String st3 = "";
st3 = st3 + z;
t3.setText(st3);
operation.setText("+");
}
} //
private class Handler2 implements ActionListener // anonymous inner class
{
public void actionPerformed(ActionEvent e) {
String st1 = t1.getText();
String st2 = t2.getText();
int x = Integer.parseInt(st1);
int y = Integer.parseInt(st2);
int z = x - y;
String st3 = "";
st3 = st3 + z;
t3.setText(st3);
operation.setText("-");
}
} // end anonymous inner class
private class Handler3 implements ActionListener // anonymous inner class
{
public void actionPerformed(ActionEvent e) {
String st1 = t1.getText();
String st2 = t2.getText();
int x = Integer.parseInt(st1);
int y = Integer.parseInt(st2);
int z = x * y;
String st3 = "";
st3 = Integer.toString(z);
t3.setText(st3);
operation.setText("*");
}
} // end anonymous inner class
private class Handler4 implements ActionListener // anonymous inner class
{
public void actionPerformed(ActionEvent e) {
t1.setText("");
t2.setText("");
t3.setText("");
operation.setText("OPR");
}
} // end anonymous inner class
public static void main(String[] args) {
ConvertFrame convertFrame = new ConvertFrame();
convertFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
convertFrame.setSize(500, 400); // set frame size
convertFrame.setVisible(true); // display frame
}
} // end class ConvertFrame
可以替换为c.gridwidth = 4;