- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
与 this question 有一定关系.
我正在尝试创建一个包含两个面板的窗口,其宽度比例为 2:1(左侧面板比右侧面板宽),并且我正在使用 GridBagLayout。
基本上,看起来像这样:
____________________| | || leftPanel |right- || |Panel || |->| ||_________|__|_______| | | both panels separated by a vertical separator
Here is the code that creates the top-level JPanel
(I've called it totalGui
) holding both leftPanel
and rightPanel
:
private JPanel createContentPane(){ //creates a top-level JPanel
//that is returned to the JFrame's
//setContentPane() method
int WINHEIGHT = 600;
int WINWIDTH = 800;
JPanel totalGui = new JPanel(); //this is the JPanel that will be returned
totalGui.setBackground(Color.white);
totalGui.setMinimumSize(new Dimension(WINWIDTH,WINHEIGHT));
totalGui.setPreferredSize(new Dimension(WINWIDTH,WINHEIGHT));
totalGui.setMaximumSize(new Dimension(WINWIDTH,WINHEIGHT));
totalGui.setLayout(new GridBagLayout());
totalGui.setOpaque(true);
GridBagConstraints c = new GridBagConstraints();
/* totalGui (the top-level JPane) has two JPanes called
leftPanel and rightPanel. The widths of leftPanel and rightPanel
are in the ratio 2:1. So, leftPanel takes up 2/3rd of the totalGui's
width, and rightPanel gets the remaining 1/3rd */
/* Top-level left panel */
JPanel leftPanel = new JPanel();
int LEFTPANELWIDTH = WINWIDTH*2/3;
leftPanel.setMinimumSize(new Dimension(LEFTPANELWIDTH,WINHEIGHT));
leftPanel.setPreferredSize(new Dimension(LEFTPANELWIDTH,WINHEIGHT));
leftPanel.setMaximumSize(new Dimension(LEFTPANELWIDTH,WINHEIGHT));
c.gridx = 0;
c.gridy = 0;
c.gridheight=4; //both leftPanel and rightPanel are 4 cells high
c.gridwidth=2; //leftPanel is 2 cells wide, rightPanel is 1 cell wide (code below)
c.fill = GridBagConstraints.VERTICAL;
totalGui.add(leftPanel, c);
/* Vertical separator between leftPanel and rightPanel */
c.gridx = 2; //'2' because leftPanel is 2 cells wide and the separator's leading
//edge must lie in the third cell (gridx = 2) if it is to be
//sandwiched between leftPanel and rightPanel
c.gridy = 0;
c.gridwidth=0; //I've also tried gridwidth=1, but it doesn't solve the problem
c.fill = GridBagConstraints.NONE;
c.anchor=GridBagConstraints.CENTER;
JSeparator verticalsep = new JSeparator(SwingConstants.VERTICAL);
verticalsep.setPreferredSize(new Dimension(6,WINHEIGHT-10)); //the 'WINHEIGHT-10' bit is purely cosmetic
//I just wanted some space between the
//separator edges and the window border
//(also why the anchor is set to CENTER)
totalGui.add(verticalsep,c); //PROBLEM: verticalsep is not displayed where I expected it to be;
//please see attached image in post
/* Top-level right panel */
JPanel rightPanel = new JPanel();
int RIGHTPANELWIDTH = WINWIDTH*1/3;
rightPanel.setBackground(Color.blue);
rightPanel.setMinimumSize(new Dimension(RIGHTPANELWIDTH,WINHEIGHT));
rightPanel.setPreferredSize(new Dimension(RIGHTPANELWIDTH,WINHEIGHT));
rightPanel.setMaximumSize(new Dimension(RIGHTPANELWIDTH,WINHEIGHT));
c.gridx = 2;
c.gridy = 0;
c.gridwidth=1; //rightPanel is 1 cell wide, leftPanel is 2 cells wide (code above)
c.gridheight=4; //both leftPanel and rightPanel are 4 cells high
c.fill = GridBagConstraints.VERTICAL;
totalGui.add(rightPanel, c);
return totalGui;
}
我的问题是分隔符没有显示在正确的位置。这是我得到的:link to image (抱歉,我没有足够的声誉来发布图片)。
谁能告诉我我做错了什么?
非常感谢您提供的任何帮助。
最佳答案
您应该使用 GridBagConstraints.HORIZONTAL
而不是 GridBagConstraints.VERTICAL
。尝试一下这个,您将得到以下结果(分隔符为红色):
package com.denisk.fun.swing;
import java.awt.*;
import javax.swing.*;
/**
* Date: Mar 6, 2011
* Time: 9:11:03 AM
*
* @author denisk
*/
public class GridBagLayoutTest extends JFrame {
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
frame.setContentPane(createContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static Container createContentPane() {
int WINHEIGHT = 600;
int WINWIDTH = 800;
JPanel totalGui = new JPanel(); //this is the JPanel that will be returned
totalGui.setBackground(Color.white);
totalGui.setMinimumSize(new Dimension(WINWIDTH, WINHEIGHT));
totalGui.setPreferredSize(new Dimension(WINWIDTH, WINHEIGHT));
totalGui.setMaximumSize(new Dimension(WINWIDTH, WINHEIGHT));
totalGui.setLayout(new GridBagLayout());
totalGui.setOpaque(true);
GridBagConstraints c = new GridBagConstraints();
/* totalGui (the top-level JPane) has two JPanes called
leftPanel and rightPanel. The widths of leftPanel and rightPanel
are in the ratio 2:1. So, leftPanel takes up 2/3rd of the totalGui's
width, and rightPanel gets the remaining 1/3rd */
/* Top-level left panel */
JPanel leftPanel = new JPanel();
int LEFTPANELWIDTH = WINWIDTH * 2 / 3;
leftPanel.setMinimumSize(new Dimension(LEFTPANELWIDTH, WINHEIGHT));
leftPanel.setPreferredSize(new Dimension(LEFTPANELWIDTH, WINHEIGHT));
leftPanel.setMaximumSize(new Dimension(LEFTPANELWIDTH, WINHEIGHT));
c.gridx = 0;
c.gridy = 0;
c.gridheight = 4; //both leftPanel and rightPanel are 4 cells high
c.gridwidth = 2; //leftPanel is 2 cells wide, rightPanel is 1 cell wide (code below)
c.fill = GridBagConstraints.HORIZONTAL;
totalGui.add(leftPanel, c);
/* Vertical separator between leftPanel and rightPanel */
c.gridx = 2; //'2' because leftPanel is 2 cells wide and the separator's leading
//edge must lie in the third cell (gridx = 2) if it is to be
//sandwiched between leftPanel and rightPanel
c.gridy = 0;
c.gridwidth = 0; //I've also tried gridwidth=1, but it doesn't solve the problem
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
JSeparator verticalsep = new JSeparator(SwingConstants.VERTICAL);
verticalsep.setBackground(new Color(255, 0, 102));
verticalsep.setPreferredSize(new Dimension(6, WINHEIGHT - 10)); //the 'WINHEIGHT-10' bit is purely cosmetic
//I just wanted some space between the
//separator edges and the window border
//(also why the anchor is set to CENTER)
totalGui.add(verticalsep, c); //PROBLEM: verticalsep is not displayed where I expected it to be;
//please see attached image in post
/* Top-level right panel */
JPanel rightPanel = new JPanel();
int RIGHTPANELWIDTH = WINWIDTH * 1 / 3;
rightPanel.setBackground(Color.blue);
rightPanel.setMinimumSize(new Dimension(RIGHTPANELWIDTH, WINHEIGHT));
rightPanel.setPreferredSize(new Dimension(RIGHTPANELWIDTH, WINHEIGHT));
rightPanel.setMaximumSize(new Dimension(RIGHTPANELWIDTH, WINHEIGHT));
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 1; //rightPanel is 1 cell wide, leftPanel is 2 cells wide (code above)
c.gridheight = 4; //both leftPanel and rightPanel are 4 cells high
c.fill = GridBagConstraints.HORIZONTAL;
totalGui.add(rightPanel, c);
return totalGui;
}
}
关于java - GridBagLayout 未正确定位垂直 JSeparator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5209192/
我在我的 java swing 应用程序中使用 JSeparator。正常执行使分隔线正常;但我需要的是分隔符应该是虚线(就像我们创建虚线边框一样)。我们有什么办法可以做到这一点吗? 谢谢 最佳答案
我想将 JSeparator 放在工具栏的各个部分之间。当我添加以下内容时,分隔符可见: toolbar.add(radioButtonDDA, BorderLayout.NORTH);
如何在 Java Swing 中创建带有标题标签的水平 JSeparator? 像这样: --- Title XYZ -------------------- 最佳答案 我找到了一个解决方案:Swin
在我的代码中,我添加了一些 JSeparator。一切工作正常,除了一开始,我必须添加两个 JSeparator 来显示它们,而不是一个!代码如下: setLayout(new BoxLayo
与 this question 有一定关系. 我正在尝试创建一个包含两个面板的窗口,其宽度比例为 2:1(左侧面板比右侧面板宽),并且我正在使用 GridBagLayout。 基本上,看起来像这样:
问题在标题中。 我目前正在做类似的事情: jSperator = new JSeparator(); jSeparator1.setForeground(new java.awt.Color(255,
我正在尝试在我的菜单中添加一个垂直的 JSeparator。但它在我的 MenuItems 之间留有边距。我希望我的项目保持在左侧而不是左 - 中 - 右。 外观: 我想要的是: JMenu s
我有这个代码: JPanel jpMainExample = new JPanel(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT)); jp
我想向 JList 添加一些 JSeparators。我正在使用 DefaultListModel,当我尝试时: ((DefaultListModel)myListModel).addElement(
我有这个小问题。我正在使用 Netbeans。当我点击 Preview Design 时,我看到了这样的 jSeparators: 但是当我运行这个项目时,它看起来是这样的: 我该如何解决这个问题?我
当我想为 Java Swing 应用程序插入带有 JSeparator 的分隔符时,我遇到了问题。事实上,我在 Mac 上运行我的程序时遇到了这个问题,但在 Windows 或 Linux 上却没有。
我使用 BorderLayout 放置了两个不同的 JButton,一个在左边(西),一个在右边(东),中间有一个水平的 JSeparator。我想要做的是将分隔符 y 对齐到中心,而不是像现在这样对
所以我的代码如下: JPanel mainPanel = new JPanel(); mainPanel.setBorder(new EmptyBorder(50,50,0,10)); BoxLayo
我想使用 GridBagLayout 在两个组件之间添加一个垂直的 JSeparator。我的代码如下: public MainWindowBody(){ setLayout(new Grid
我正在尝试使用 Swing 小部件将两个按钮放入面板中。在 NetBeans IDE 中,我的 JSeparator border 属性在属性 Pane 中设置为 (No border)。 然而,一行
我在自定义 jdialog 中使用 JSeparator 这是我的代码: public class CheckDialog extends javax.swing.JDialog { priv
我正在构建一个应用程序来记录订单并为这些订单生成发票。我使用 jSeparator 将客户信息与订单信息分开。但我想增加分隔符的高度并改变它的颜色。任何帮助将不胜感激。 我已经尝试过使用 setSiz
我想在 GridBagLayout 中的每个 JLabel 之间有一个 JSeparator。目前它看起来像这样: 现在我想在图标和 JLabel 之间的每个 TESTSTEP 标签之后添加一个 JS
我在 Java Swing 中遇到了一个简单的问题。我将我的代码简化为以下代码片段。我不确定如何最小化水平 JSeparator 与下一个 JTextField 之间的间隙大小,因为当前代码在两者之间
这是我尝试根据 How to change the color of a JSeparator? 中的答案将垂直 JSeparator 的颜色从 Nimbus 默认黑色更改为红色的尝试。 : publ
我是一名优秀的程序员,十分优秀!