没有空白的GUI显示为“拥挤”。我如何提供空白而不需要明确地设置元件的位置或尺寸?…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
使用各种layoutmanagers
可以提供不同组件之间的间距。
>
getter和setter方法
对于水平间距:borderLayout.gethGap()和borderLayout.sethGap(int hgap)
对于垂直间距:flowLayout.getVGap()和flowLayout.setVGap()
>
getter和setter方法
对于水平间距:GridLayout.GethGap()和GridLayout.SethGap(int hgap)
卡片布局(int hGap,int vGap)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LayoutExample {
private final int hGap = 5;
private final int vGap = 5;
private String[] borderConstraints = {
BorderLayout.PAGE_START,
BorderLayout.LINE_START,
BorderLayout.CENTER,
BorderLayout.LINE_END,
BorderLayout.PAGE_END
};
private JButton[] buttons;
private GridBagConstraints gbc;
private JPanel borderPanel;
private JPanel flowPanel;
private JPanel gridPanel;
private JPanel gridBagPanel;
private JPanel cardPanel;
public LayoutExample() {
buttons = new JButton[16];
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(hGap, vGap, hGap, vGap);
}
private void displayGUI() {
JFrame frame = new JFrame("Layout Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(
new GridLayout(0, 1, hGap, vGap));
contentPane.setBorder(
BorderFactory.createEmptyBorder(hGap, vGap, hGap, vGap));
borderPanel = new JPanel(new BorderLayout(hGap, vGap));
borderPanel.setBorder(
BorderFactory.createTitledBorder("BorderLayout"));
borderPanel.setOpaque(true);
borderPanel.setBackground(Color.WHITE);
for (int i = 0; i < 5; i++) {
buttons[i] = new JButton(borderConstraints[i]);
borderPanel.add(buttons[i], borderConstraints[i]);
}
contentPane.add(borderPanel);
flowPanel = new JPanel(new FlowLayout(
FlowLayout.CENTER, hGap, vGap));
flowPanel.setBorder(
BorderFactory.createTitledBorder("FlowLayout"));
flowPanel.setOpaque(true);
flowPanel.setBackground(Color.WHITE);
for (int i = 5; i < 8; i++) {
buttons[i] = new JButton(Integer.toString(i));
flowPanel.add(buttons[i]);
}
contentPane.add(flowPanel);
gridPanel = new JPanel(new GridLayout(2, 2, hGap, vGap));
gridPanel.setBorder(
BorderFactory.createTitledBorder("GridLayout"));
gridPanel.setOpaque(true);
gridPanel.setBackground(Color.WHITE);
for (int i = 8; i < 12; i++) {
buttons[i] = new JButton(Integer.toString(i));
gridPanel.add(buttons[i]);
}
contentPane.add(gridPanel);
gridBagPanel = new JPanel(new GridBagLayout());
gridBagPanel.setBorder(
BorderFactory.createTitledBorder("GridBagLayout"));
gridBagPanel.setOpaque(true);
gridBagPanel.setBackground(Color.WHITE);
buttons[12] = new JButton(Integer.toString(12));
addComp(gridBagPanel, buttons[12], 0, 0, 1, 1
, GridBagConstraints.BOTH, 0.33, 0.5);
buttons[13] = new JButton(Integer.toString(13));
addComp(gridBagPanel, buttons[13], 1, 0, 1, 1
, GridBagConstraints.BOTH, 0.33, 0.5);
buttons[14] = new JButton(Integer.toString(14));
addComp(gridBagPanel, buttons[14], 0, 1, 2, 1
, GridBagConstraints.BOTH, 0.66, 0.5);
buttons[15] = new JButton(Integer.toString(15));
addComp(gridBagPanel, buttons[15], 2, 0, 1, 2
, GridBagConstraints.BOTH, 0.33, 1.0);
contentPane.add(gridBagPanel);
cardPanel = new JPanel(new CardLayout(hGap, vGap));
cardPanel.setBorder(
BorderFactory.createTitledBorder("CardLayout"));
cardPanel.setOpaque(true);
cardPanel.setBackground(Color.WHITE);
cardPanel.add(getPanel(Color.BLUE));
cardPanel.add(getPanel(Color.GREEN));
contentPane.add(cardPanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel getPanel(Color bColor) {
JPanel panel = new JPanel(new FlowLayout(
FlowLayout.CENTER, hGap, vGap));
panel.setOpaque(true);
panel.setBackground(bColor.darker().darker());
JButton swapperButton = new JButton("Next");
swapperButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
CardLayout cardLayout = (CardLayout) cardPanel.getLayout();
cardLayout.next(cardPanel);
}
});
panel.add(swapperButton);
return panel;
}
private void addComp(JPanel panel, JComponent comp
, int x, int y, int gWidth
, int gHeight, int fill
, double weightx, double weighty) {
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = gWidth;
gbc.gridheight = gHeight;
gbc.fill = fill;
gbc.weightx = weightx;
gbc.weighty = weighty;
panel.add(comp, gbc);
}
public static void main(String[] args) {
Runnable runnable = new Runnable(){
@Override
public void run() {
new LayoutExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
我一直试图使这个应用程序,将产生一个折线图后,我点击"添加新数据项"按钮。现在,在我点击按钮后,什么都没发生(gui冻结),在我最大化框架后,图形出现在框架内,这意味着我的程序工作了,但我不知道为什么我的gui冻结了。我见过类似的问题,人们回答说必须引入一个新的线程来处理不同的拍摄,我也尝试过,但它仍然不起作用,只是让情况变得更糟。有人知道我犯了什么错误吗?这是我的代码: 谢谢你!
在hibernate中,我在Dao层中使用了和方法按照我的预期工作,但总是返回空结果集,我仍然无法找出原因,任何人都可以帮助我解决这个问题, 注意:没有异常,只返回空列表 这是我的代码 查找全部方法 通过id方法查找 getSession方法 提前谢谢大家干杯
版本: 离子型4,类型=角型,cordova v7。0 当我试图运行命令:离子科尔多瓦运行Android我的应用程序运行模拟器完美(设备版本7.1 API 25),但当我试图安装应用程序到我的物理设备版本4.4.2(API 19)我看到空白屏幕。 在我的配置中。xml文件我看到: 我能做什么呢? 其他详细信息从调试与Android Studio:
我有一个xslt 2.0文件,用于将csv文件转换为xml文件。xsl是从这里获取的:http://P2P . wrox . com/XSLT/40898-transform-CSV-file-XML . html # post 164344 现在我正在尝试通过Java变压器(使用Saxon9 xsl变压器工厂)执行此操作。由于csv文件作为参数传递到xsl中,因此我不需要在转换方法中的Sourc
我跟着http://developer.android.com/guide/google/gcm/gs.html#server-在我的应用程序中实现GCM的应用程序 返回空字符串作为注册ID,还需要什么来获得注册ID??
我做了一个主菜单JFrame,它可以生成4个不同的新框架来表示每个菜单。主菜单框架工作正常。但是我发现它不能从新框架文本字段中获取文本。这是错误报告和源代码。请让我知道如何修复它。 java线程“AWT-EventQueue-0”中出现异常。电话簿上的lang.NullPointerException。在javax上执行的操作(PhoneBook.java:166)。摆动抽象按钮。fireActi