[英]java swing calculator layouts
我需要做一個作業並創建一個計算器。 這是一門Java初學者課程,因此請記住,我不是專家。 它看起來不應該很壯觀,因此實現以下目標的最簡單方法就是很棒。
它的內部工作原理很好,但是繪制它確實讓人頭疼。
到目前為止,我們只接觸了布局...在這種情況下,這根本不是我想要的。 首先讓我告訴您要尋找的布局:
我嘗試在此處繪制,但格式不正確。 如果我可以將其放入HTML中,它將基本上是一個簡單的表,具有6行2列。 但是第一行必須跨兩列。
Flowlayout
只是將所有內容從左到右彼此相鄰。
在那之后,我嘗試使用GridLayout
,但是最上面的標題是這里的問題,因為它沒有跨越兩列。
到目前為止,這是我的代碼:
public class TripCalculator extends JFrame implements ActionListener {
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
public static final int NUMBER_OF_CHAR = 4;
public JTextField stopTime, distance, tripTime, speed;
public TripCalculator() {
setSize(WIDTH, HEIGHT);
WindowDestroyer listener = new WindowDestroyer();
addWindowListener(listener);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
JLabel heading = new JLabel("HEADING");
contentPane.add(heading);
contentPane.setLayout(new FlowLayout());
JButton addStop = new JButton("BUTTON1");
addStop.addActionListener(this);
JButton addLeg = new JButton("BUTTON2");
addLeg.addActionListener(this);
contentPane.add(addStop);
contentPane.add(addLeg);
JLabel subHead1 = new JLabel("LABEL1");
contentPane.add(subHead1);
JLabel subHead2 = new JLabel("LABEL2");
contentPane.add(subHead2);
stopTime = new JTextField(NUMBER_OF_CHAR);
contentPane.add(stopTime);
distance = new JTextField(NUMBER_OF_CHAR);
contentPane.add(distance);
JLabel subHead3 = new JLabel("LABEL3");
contentPane.add(subHead3);
JLabel subHead4 = new JLabel("LABEL4");
contentPane.add(subHead4);
tripTime = new JTextField(NUMBER_OF_CHAR);
contentPane.add(tripTime);
speed = new JTextField(NUMBER_OF_CHAR);
contentPane.add(speed);
}
}
如果有人能向我展示正確的方向,我將不勝感激。
Flowlayout或Gridlayout本身不會幫助您。 您可以使用Gridbaglayout,也可以使用諸如FlowLaout + Gridlayout之類的布局組合。
如果您正在繪制計算器,我假設您正在繪制如下內容:
在頂部,您有標題信息,在中間,有計算器鍵盤,在底部有一些其他按鈕:
這可以通過垂直框的布局來實現,頂部和底部具有流布局,中間是帶有所有數字鍵的網格布局。
但是...如果沒有顯示想要的圖,很難說。
這是一個使用多個Layout Managers
器的示例,您可以看到可以使用多個Layout Managers
,但是應該使用多個JPanel
來實現所需的功能。
另外一個建議是: 不要從JFrame的延伸,而不是創建一個JFrame
對象,像我一樣在這個例子中 ,並在這里就是為什么你不應該這樣做。
import java.awt.*;
import javax.swing.*;
public class LayoutManagersExample {
public static void main(String args[]) {
new LayoutManagersExample();
}
public LayoutManagersExample() {
JFrame frame = new JFrame("Layout Managers Example");
JPanel topPane = new JPanel();
JPanel midPane = new JPanel();
JPanel panesHolder = new JPanel();
JLabel label = new JLabel("Top label");
JTextField field = new JTextField();
field.setColumns(5);
topPane.setLayout(new FlowLayout());
midPane.setLayout(new GridLayout(3, 2));
topPane.add(label);
topPane.add(field);
midPane.add(new JButton("Button 1"));
midPane.add(new JButton("Button 2"));
midPane.add(new JButton("Hello I'm a button"));
midPane.add(new JButton("HEY! Click me :)"));
midPane.add(new JButton("I love you"));
midPane.add(new JButton("This is another button"));
panesHolder.setLayout(new BoxLayout(panesHolder, BoxLayout.Y_AXIS));
panesHolder.add(topPane);
panesHolder.add(midPane);
frame.add(panesHolder);
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
}
這是這樣的:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.