- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要对齐时间,使其看起来像这样:
到目前为止,我已经在这段代码中实现了它:(这是我的代码的一个简短版本)
package cardlayoutalignment;
import java.awt.EventQueue;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
public class gridbaglayoutdemo {
JFrame Card = new JFrame();
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT,2,2);
Border etch = BorderFactory.createEtchedBorder(Color.white,Color.gray);
Border margin = new EmptyBorder(10,10,10,10);
public static GridBagLayout grid = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
final static boolean shouldFill = true;
JPanel container = new JPanel();
JPanel divider = new JPanel();
JPanel bodypanel = new JPanel();
final JPanel buttonpanel = new JPanel();
JPanel panel_1 = new JPanel();
JPanel panel_4 = new JPanel();
CardLayout cl = new CardLayout();
public gridbaglayoutdemo(){
Card.setVisible(true);
Card.setSize(605,333);
Card.setTitle("Tank Delivery");
//Card.setResizable(false);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x=(int)((dimension.getWidth() - Card.getWidth())/2);
int y=(int)((dimension.getHeight() - Card.getHeight())/2);
Card.setLocation(x, y);
Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
bodypanel.setLayout(new BorderLayout());
divider.setLayout(new BorderLayout());
container.setLayout(cl);
cl.show(container, "1");
//JOptionPane.showMessageDialog(null, "Memory is full. Finish 1 transaction in order to use Memory again");
panel_1.setLayout(grid);
JLabel label_1 = new JLabel("Enter Tank:");
label_1.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,0,0);
panel_1.add(label_1, c);
JComboBox box_1 = new JComboBox();
box_1.setPreferredSize(new Dimension(200,30));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 20;
c.insets = new Insets(10,10,0,0);
panel_1.add(box_1,c);
JLabel label = new JLabel("");
label.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 1;
c.gridx = 0;
c.gridy = 30;
c.insets = new Insets(10,0,0,0);
panel_1.add(label, c);
panel_4.setLayout(grid);
JLabel label_2 = new JLabel("Enter Date:");
label_2.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10,10,0,0);
panel_4.add(label_2,c);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
df.setLenient(false);
JFormattedTextField text_2 = new JFormattedTextField(df);
text_2.setValue(new Date());
text_2.setPreferredSize(new Dimension(200,30));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 5;
c.insets = new Insets(10,10,0,0);
panel_4.add(text_2,c);
JLabel label_3 = new JLabel("Enter Date:");
label_3.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 10;
c.insets = new Insets(10,10,0,10);
panel_4.add(label_3,c);
JLabel text_3 = new JLabel("");
text_3.setPreferredSize(new Dimension(0,30));
c.fill = GridBagConstraints.NONE;
c.weightx = 0;
c.weighty = 0;
c.gridx = 0;
c.gridy = 15;
c.insets = new Insets(0,0,0,0);
panel_4.add(text_3,c);
String time3 = new java.text.SimpleDateFormat("HH").format(new java.util.Date() );
JFormattedTextField text_33 = new JFormattedTextField();
text_33.setValue(time3);
//text_33.setPreferredSize(new Dimension(30,30));
c.fill = GridBagConstraints.NONE;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 2;
c.gridy = 15;
c.insets = new Insets(0,0,0,0);
panel_4.add(text_33,c);
JLabel label_4 = new JLabel(":");
label_4.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.NONE;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 3;
c.gridy = 15;
c.insets = new Insets(0,0,0,0);
panel_4.add(label_4,c);
String time2 = new java.text.SimpleDateFormat("mm").format(new java.util.Date() );
JFormattedTextField text_4 = new JFormattedTextField();
text_4.setValue(time2);
//text_4.setPreferredSize(new Dimension(30,30));
c.fill = GridBagConstraints.NONE;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 5;
c.gridy = 15;
c.insets = new Insets(0,0,0,0);
panel_4.add(text_4,c);
JButton btnup = new JButton("UP");
btnup.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 10;
c.gridy = 15;
c.insets = new Insets(0,0,0,0);
panel_4.add(btnup, c);
JButton btndown = new JButton("DOWN");
btndown.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 10;
c.gridy = 20;
c.insets = new Insets(0,0,0,0);
panel_4.add(btndown, c);
JLabel label14 = new JLabel("");
label14.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 1;
c.gridx = 0;
c.gridy = 30;
c.insets = new Insets(10,0,0,0);
panel_4.add(label14, c);
buttonpanel.setLayout(new FlowLayout(SwingConstants.RIGHT));
buttonpanel.setBorder(new EmptyBorder(0,10,0,0));
JButton btnBack = new JButton("< Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.previous(container);
buttonpanel.repaint();
}
});
btnBack.setFont(new Font("Arial", Font.PLAIN, 20));
btnBack.setFocusable(false);
btnBack.setFocusTraversalKeysEnabled(false);
btnBack.setFocusPainted(false);
btnBack.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnBack.setPreferredSize(new Dimension(110, 40));
btnBack.setBackground(new Color(224,223,227));
buttonpanel.add(btnBack);
JButton btnNext = new JButton("Next >");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.next(container);
buttonpanel.repaint();
}
});
btnNext.setFont(new Font("Arial", Font.PLAIN, 20));
btnNext.setFocusable(false);
btnNext.setFocusTraversalKeysEnabled(false);
btnNext.setFocusPainted(false);
btnNext.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnNext.setPreferredSize(new Dimension(110, 40));
btnNext.setBackground(new Color(224,223,227));
buttonpanel.add(btnNext);
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnCancel.setFont(new Font("Arial", Font.PLAIN, 20));
btnCancel.setFocusable(false);
btnCancel.setFocusTraversalKeysEnabled(false);
btnCancel.setFocusPainted(false);
btnCancel.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnCancel.setPreferredSize(new Dimension(110, 40));
btnCancel.setBackground(new Color(224,223,227));
buttonpanel.add(btnCancel);
divider.add(container, BorderLayout.NORTH);
container.add(panel_1, "1");
//container.add(p2.panel_2, "2");
//container.add(p3.panel_3, "3");
container.add(panel_4, "4");
divider.add(buttonpanel, BorderLayout.SOUTH);
JPanel numberpanel = new JPanel();
numberpanel.setPreferredSize(new Dimension(221,0));
numberpanel.setBorder(new EmptyBorder(10,0,0,10));
numberpanel.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.gray));
numberpanel.setLayout(flow);
JButton button_7 = new JButton("7");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_7.setFont(new Font("Arial", Font.PLAIN, 30));
button_7.setFocusable(false);
button_7.setFocusTraversalKeysEnabled(false);
button_7.setFocusPainted(false);
button_7.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_7.setPreferredSize(new Dimension(70, 70));
button_7.setBackground(new Color(224,223,227));
numberpanel.add(button_7);
JButton button_8 = new JButton("8");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_8.setFont(new Font("Arial", Font.PLAIN, 30));
button_8.setFocusable(false);
button_8.setFocusTraversalKeysEnabled(false);
button_8.setFocusPainted(false);
button_8.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_8.setPreferredSize(new Dimension(70, 70));
button_8.setBackground(new Color(224,223,227));
numberpanel.add(button_8);
JButton button_9 = new JButton("9");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_9.setFont(new Font("Arial", Font.PLAIN, 30));
button_9.setFocusable(false);
button_9.setFocusTraversalKeysEnabled(false);
button_9.setFocusPainted(false);
button_9.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_9.setPreferredSize(new Dimension(70, 70));
button_9.setBackground(new Color(224,223,227));
numberpanel.add(button_9);
JButton button_4 = new JButton("4");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_4.setFont(new Font("Arial", Font.PLAIN, 30));
button_4.setFocusable(false);
button_4.setFocusTraversalKeysEnabled(false);
button_4.setFocusPainted(false);
button_4.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_4.setPreferredSize(new Dimension(70, 70));
button_4.setBackground(new Color(224,223,227));
numberpanel.add(button_4);
JButton button_5 = new JButton("5");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_5.setFont(new Font("Arial", Font.PLAIN, 30));
button_5.setFocusable(false);
button_5.setFocusTraversalKeysEnabled(false);
button_5.setFocusPainted(false);
button_5.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_5.setPreferredSize(new Dimension(70, 70));
button_5.setBackground(new Color(224,223,227));
numberpanel.add(button_5);
JButton button_6 = new JButton("6");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_6.setFont(new Font("Arial", Font.PLAIN, 30));
button_6.setFocusable(false);
button_6.setFocusTraversalKeysEnabled(false);
button_6.setFocusPainted(false);
button_6.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_6.setPreferredSize(new Dimension(70, 70));
button_6.setBackground(new Color(224,223,227));
numberpanel.add(button_6);
JButton button_1 = new JButton("1");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_1.setFont(new Font("Arial", Font.PLAIN, 30));
button_1.setFocusable(false);
button_1.setFocusTraversalKeysEnabled(false);
button_1.setFocusPainted(false);
button_1.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_1.setPreferredSize(new Dimension(70, 70));
button_1.setBackground(new Color(224,223,227));
numberpanel.add(button_1);
JButton button_2 = new JButton("2");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_2.setFont(new Font("Arial", Font.PLAIN, 30));
button_2.setFocusable(false);
button_2.setFocusTraversalKeysEnabled(false);
button_2.setFocusPainted(false);
button_2.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_2.setPreferredSize(new Dimension(70, 70));
button_2.setBackground(new Color(224,223,227));
numberpanel.add(button_2);
JButton button_3 = new JButton("3");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_3.setFont(new Font("Arial", Font.PLAIN, 30));
button_3.setFocusable(false);
button_3.setFocusTraversalKeysEnabled(false);
button_3.setFocusPainted(false);
button_3.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_3.setPreferredSize(new Dimension(70, 70));
button_3.setBackground(new Color(224,223,227));
numberpanel.add(button_3);
JButton button_0 = new JButton("0");
button_0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_0.setFont(new Font("Arial", Font.PLAIN, 30));
button_0.setFocusable(false);
button_0.setFocusTraversalKeysEnabled(false);
button_0.setFocusPainted(false);
button_0.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_0.setPreferredSize(new Dimension(70, 70));
button_0.setBackground(new Color(224,223,227));
numberpanel.add(button_0);
JButton button_left = new JButton("");
button_left.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_left.setIcon(new ImageIcon("more_buttons\\left.png"));
button_left.setFont(new Font("Arial", Font.PLAIN, 30));
button_left.setFocusable(false);
button_left.setFocusTraversalKeysEnabled(false);
button_left.setFocusPainted(false);
button_left.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_left.setPreferredSize(new Dimension(70, 70));
button_left.setBackground(new Color(224,223,227));
numberpanel.add(button_left);
JButton button_right = new JButton("");
button_right.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_right.setIcon(new ImageIcon("more_buttons\\right.png"));
button_right.setFont(new Font("Arial", Font.PLAIN, 30));
button_right.setFocusable(false);
button_right.setFocusTraversalKeysEnabled(false);
button_right.setFocusPainted(false);
button_right.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_right.setPreferredSize(new Dimension(70, 70));
button_right.setBackground(new Color(224,223,227));
numberpanel.add(button_right);
Card.add(bodypanel);
bodypanel.add(divider, BorderLayout.WEST);
bodypanel.add(numberpanel, BorderLayout.EAST);
}
public static void main(String[] args){
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new gridbaglayoutdemo();
}
});
}
}
我的问题是在“输入时间:”的标签下方我放了一个空白标签。当我将 JTextField
放在那里时,我无法像上图那样对齐它,因为它的单元格正与它上面的 JLabel
对齐。我已经尝试了 gridwidth
、gridheight
(- 用于向上和向下按钮)、weightx
、insets
和添加一个空白 JLabel
(- 以占据行尾的空间)。而这一切都失败了。我不知道我做错了什么。我也知道我不必调用 setPreferredSize
但我必须调用,因为我希望时间字段获得与上图相同的宽度。我认为向上和向下按钮的对齐方式不会像图片中那样,因为在 GridBagLayout
中没有中间位置。好吧,我已经尝试将时间字段的 gridheight
设置为 2,结果仍然是那样。有没有办法像图片中那样对齐这些字段?
最佳答案
我认为您不知道 GridBagLayout 与 Grid NxM 一起使用,这意味着同一列中的所有组件都具有相同宽度的单元格,并且同一行中的所有组件都位于相同高度的单元格中。它只是一个网格。
问题很简单,第 0 列中有一些组件:“输入日期:”标签、输入日期 TextField...所以最宽的组件决定了该列的大小。现在你想要放置一堆文本字段、标签和按钮来表示 [14:00 Up/down]。您现在的做法是首先放置一个实际上不执行任何操作(除了使用其单元格)的空标签,然后将所述组件放入第 2、3、4 列...
然而,正如我所说,第 0 列非常宽,因此第 1+ 列从已经放置在那里的所有内容的右侧开始,这就是为什么所有这些组件都出现在“右侧”的原因,因为第 0 列非常宽.
现在要将它们放在正确的位置,有 2 个选项。要么让上面的所有这些组件跨越几列,这样您就可以使用这些列将您需要的所有内容放在这些组件下,或者创建一个包含这些组件的面板,然后将此面板放在“输入时间:”下的第 0 列中:
在我看来,第二种方法更好,所以我定义了一个 timeInputPanel
,用我们想要的组件填充它,然后将它放在 panel_4
的第 0 列中>。现在因为 timeInputPanel
只是 1 个组件,它可以毫无问题地驻留在列 0 内,即使它包含几个其他组件也是如此。
相关代码就是这样:
JPanel timeInputPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
timeInputPanel.add(text_33);
timeInputPanel.add(label_4);
timeInputPanel.add(text_4);
timeInputPanel.add(upDownPanel);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 15;
c.insets = new Insets(0, 10, 0, 0);
panel_4.add(timeInputPanel, c);
当然,您可能希望根据自己的喜好更改 GridBagConstraints,唯一重要的是 c.gridx = 0;
和适当的 gridy
。
看起来像这样:
完整代码:
package cardlayoutalignment;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
public class gridbaglayoutdemo
{
JFrame Card = new JFrame();
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT, 2, 2);
Border etch = BorderFactory.createEtchedBorder(Color.white, Color.gray);
Border margin = new EmptyBorder(10, 10, 10, 10);
public static GridBagLayout grid = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
final static boolean shouldFill = true;
JPanel container = new JPanel();
JPanel divider = new JPanel();
JPanel bodypanel = new JPanel();
final JPanel buttonpanel = new JPanel();
JPanel panel_1 = new JPanel();
JPanel panel_4 = new JPanel();
CardLayout cl = new CardLayout();
public gridbaglayoutdemo()
{
Card.setVisible(true);
Card.setSize(605, 333);
Card.setTitle("Tank Delivery");
// Card.setResizable(false);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - Card.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - Card.getHeight()) / 2);
Card.setLocation(x, y);
Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
bodypanel.setLayout(new BorderLayout());
divider.setLayout(new BorderLayout());
container.setLayout(cl);
cl.show(container, "1");
// JOptionPane.showMessageDialog(null,
// "Memory is full. Finish 1 transaction in order to use Memory again");
panel_1.setLayout(grid);
JLabel label_1 = new JLabel("Enter Tank:");
label_1.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10, 10, 0, 0);
panel_1.add(label_1, c);
JComboBox box_1 = new JComboBox();
box_1.setPreferredSize(new Dimension(200, 30));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 20;
c.insets = new Insets(10, 10, 0, 0);
panel_1.add(box_1, c);
JLabel label = new JLabel("");
label.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 1;
c.gridx = 0;
c.gridy = 30;
c.insets = new Insets(10, 0, 0, 0);
panel_1.add(label, c);
panel_4.setLayout(grid);
JLabel label_2 = new JLabel("Enter Date:");
label_2.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(10, 10, 0, 0);
panel_4.add(label_2, c);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
df.setLenient(false);
JFormattedTextField text_2 = new JFormattedTextField(df);
text_2.setValue(new Date());
text_2.setPreferredSize(new Dimension(200, 30));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 5;
c.insets = new Insets(10, 10, 0, 0);
panel_4.add(text_2, c);
JLabel label_3 = new JLabel("Enter Time:");
label_3.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 10;
c.insets = new Insets(10, 10, 0, 10);
panel_4.add(label_3, c);
String time3 = new java.text.SimpleDateFormat("HH").format(new java.util.Date());
JFormattedTextField text_33 = new JFormattedTextField();
text_33.setValue(time3);
JLabel label_4 = new JLabel(":");
label_4.setFont(new Font("Arial", Font.PLAIN, 18));
String time2 = new java.text.SimpleDateFormat("mm").format(new java.util.Date());
JFormattedTextField text_4 = new JFormattedTextField();
text_4.setValue(time2);
JButton btnup = new JButton("UP");
btnup.setFont(new Font("Arial", Font.PLAIN, 18));
JButton btndown = new JButton("DOWN");
btndown.setFont(new Font("Arial", Font.PLAIN, 18));
JPanel upDownPanel = new JPanel(new BorderLayout());
upDownPanel.add(btnup, BorderLayout.PAGE_START);
upDownPanel.add(btndown, BorderLayout.PAGE_END);
JPanel timeInputPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
timeInputPanel.add(text_33);
timeInputPanel.add(label_4);
timeInputPanel.add(text_4);
timeInputPanel.add(upDownPanel);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 15;
c.insets = new Insets(0, 10, 0, 0);
panel_4.add(timeInputPanel, c);
JLabel label14 = new JLabel("");
label14.setFont(new Font("Arial", Font.PLAIN, 18));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 1;
c.gridx = 0;
c.gridy = 30;
c.insets = new Insets(10, 0, 0, 0);
panel_4.add(label14, c);
buttonpanel.setLayout(new FlowLayout(SwingConstants.RIGHT));
buttonpanel.setBorder(new EmptyBorder(0, 10, 0, 0));
JButton btnBack = new JButton("< Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
cl.previous(container);
buttonpanel.repaint();
}
});
btnBack.setFont(new Font("Arial", Font.PLAIN, 20));
btnBack.setFocusable(false);
btnBack.setFocusTraversalKeysEnabled(false);
btnBack.setFocusPainted(false);
btnBack.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnBack.setPreferredSize(new Dimension(110, 40));
btnBack.setBackground(new Color(224, 223, 227));
buttonpanel.add(btnBack);
JButton btnNext = new JButton("Next >");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
cl.next(container);
buttonpanel.repaint();
}
});
btnNext.setFont(new Font("Arial", Font.PLAIN, 20));
btnNext.setFocusable(false);
btnNext.setFocusTraversalKeysEnabled(false);
btnNext.setFocusPainted(false);
btnNext.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnNext.setPreferredSize(new Dimension(110, 40));
btnNext.setBackground(new Color(224, 223, 227));
buttonpanel.add(btnNext);
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
btnCancel.setFont(new Font("Arial", Font.PLAIN, 20));
btnCancel.setFocusable(false);
btnCancel.setFocusTraversalKeysEnabled(false);
btnCancel.setFocusPainted(false);
btnCancel.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
btnCancel.setPreferredSize(new Dimension(110, 40));
btnCancel.setBackground(new Color(224, 223, 227));
buttonpanel.add(btnCancel);
divider.add(container, BorderLayout.NORTH);
container.add(panel_1, "1");
// container.add(p2.panel_2, "2");
// container.add(p3.panel_3, "3");
container.add(panel_4, "4");
divider.add(buttonpanel, BorderLayout.SOUTH);
JPanel numberpanel = new JPanel();
numberpanel.setPreferredSize(new Dimension(221, 0));
numberpanel.setBorder(new EmptyBorder(10, 0, 0, 10));
numberpanel.setBorder(BorderFactory.createEtchedBorder(Color.white, Color.gray));
numberpanel.setLayout(flow);
JButton button_7 = new JButton("7");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button_7.setFont(new Font("Arial", Font.PLAIN, 30));
button_7.setFocusable(false);
button_7.setFocusTraversalKeysEnabled(false);
button_7.setFocusPainted(false);
button_7.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_7.setPreferredSize(new Dimension(70, 70));
button_7.setBackground(new Color(224, 223, 227));
numberpanel.add(button_7);
JButton button_8 = new JButton("8");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button_8.setFont(new Font("Arial", Font.PLAIN, 30));
button_8.setFocusable(false);
button_8.setFocusTraversalKeysEnabled(false);
button_8.setFocusPainted(false);
button_8.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_8.setPreferredSize(new Dimension(70, 70));
button_8.setBackground(new Color(224, 223, 227));
numberpanel.add(button_8);
JButton button_9 = new JButton("9");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button_9.setFont(new Font("Arial", Font.PLAIN, 30));
button_9.setFocusable(false);
button_9.setFocusTraversalKeysEnabled(false);
button_9.setFocusPainted(false);
button_9.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_9.setPreferredSize(new Dimension(70, 70));
button_9.setBackground(new Color(224, 223, 227));
numberpanel.add(button_9);
JButton button_4 = new JButton("4");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button_4.setFont(new Font("Arial", Font.PLAIN, 30));
button_4.setFocusable(false);
button_4.setFocusTraversalKeysEnabled(false);
button_4.setFocusPainted(false);
button_4.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_4.setPreferredSize(new Dimension(70, 70));
button_4.setBackground(new Color(224, 223, 227));
numberpanel.add(button_4);
JButton button_5 = new JButton("5");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button_5.setFont(new Font("Arial", Font.PLAIN, 30));
button_5.setFocusable(false);
button_5.setFocusTraversalKeysEnabled(false);
button_5.setFocusPainted(false);
button_5.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_5.setPreferredSize(new Dimension(70, 70));
button_5.setBackground(new Color(224, 223, 227));
numberpanel.add(button_5);
JButton button_6 = new JButton("6");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button_6.setFont(new Font("Arial", Font.PLAIN, 30));
button_6.setFocusable(false);
button_6.setFocusTraversalKeysEnabled(false);
button_6.setFocusPainted(false);
button_6.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_6.setPreferredSize(new Dimension(70, 70));
button_6.setBackground(new Color(224, 223, 227));
numberpanel.add(button_6);
JButton button_1 = new JButton("1");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button_1.setFont(new Font("Arial", Font.PLAIN, 30));
button_1.setFocusable(false);
button_1.setFocusTraversalKeysEnabled(false);
button_1.setFocusPainted(false);
button_1.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_1.setPreferredSize(new Dimension(70, 70));
button_1.setBackground(new Color(224, 223, 227));
numberpanel.add(button_1);
JButton button_2 = new JButton("2");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button_2.setFont(new Font("Arial", Font.PLAIN, 30));
button_2.setFocusable(false);
button_2.setFocusTraversalKeysEnabled(false);
button_2.setFocusPainted(false);
button_2.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_2.setPreferredSize(new Dimension(70, 70));
button_2.setBackground(new Color(224, 223, 227));
numberpanel.add(button_2);
JButton button_3 = new JButton("3");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button_3.setFont(new Font("Arial", Font.PLAIN, 30));
button_3.setFocusable(false);
button_3.setFocusTraversalKeysEnabled(false);
button_3.setFocusPainted(false);
button_3.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_3.setPreferredSize(new Dimension(70, 70));
button_3.setBackground(new Color(224, 223, 227));
numberpanel.add(button_3);
JButton button_0 = new JButton("0");
button_0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button_0.setFont(new Font("Arial", Font.PLAIN, 30));
button_0.setFocusable(false);
button_0.setFocusTraversalKeysEnabled(false);
button_0.setFocusPainted(false);
button_0.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_0.setPreferredSize(new Dimension(70, 70));
button_0.setBackground(new Color(224, 223, 227));
numberpanel.add(button_0);
JButton button_left = new JButton("");
button_left.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button_left.setIcon(new ImageIcon("more_buttons\\left.png"));
button_left.setFont(new Font("Arial", Font.PLAIN, 30));
button_left.setFocusable(false);
button_left.setFocusTraversalKeysEnabled(false);
button_left.setFocusPainted(false);
button_left.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_left.setPreferredSize(new Dimension(70, 70));
button_left.setBackground(new Color(224, 223, 227));
numberpanel.add(button_left);
JButton button_right = new JButton("");
button_right.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
button_right.setIcon(new ImageIcon("more_buttons\\right.png"));
button_right.setFont(new Font("Arial", Font.PLAIN, 30));
button_right.setFocusable(false);
button_right.setFocusTraversalKeysEnabled(false);
button_right.setFocusPainted(false);
button_right.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
button_right.setPreferredSize(new Dimension(70, 70));
button_right.setBackground(new Color(224, 223, 227));
numberpanel.add(button_right);
Card.add(bodypanel);
bodypanel.add(divider, BorderLayout.WEST);
bodypanel.add(numberpanel, BorderLayout.EAST);
}
public static void main(String[] args)
{
// Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable() {
@Override
public void run()
{
new gridbaglayoutdemo();
}
});
}
}
关于java - 如何对齐 GridbagLayout 中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24863238/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!