Tag: layout manager

获取具有固定宽度的多行文本的高度,以使对话框正确resize

我想创建一个包含某种文本元素(JLabel / JTextArea等)的对话框,它是多行的并包装单词。 我希望对话框具有固定的宽度,但根据文本的大小来调整高度。 我有这个代码: import static javax.swing.GroupLayout.DEFAULT_SIZE; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class TextSizeProblem extends JFrame { public TextSizeProblem() { String dummyString = “”; for (int i = 0; i < 100; i++) { dummyString += " word" + i; //Create a long text } JLabel text = new JLabel(); text.setText("” + […]

如何在JFrame中的任何位置设置按钮的位置

我想要做的是将按钮放在应用程序的左下角。 有人可以给我一个如何做的例子吗? 这就是我所拥有的: 这是我的代码: super(“Test”); /**Create Components**/ JPanel addPanel = new JPanel(); JButton addButton= new JButton(“Add”); /**Add Components**/ addPanel.add(addButton); this.add(addPanel); /**Set Components Properties**/ addButton.setLocation(12, 371); addButton.setPreferredSize(new Dimension(116, 40)); addPanel.setLocation(12, 371); addPanel.setPreferredSize(new Dimension(116, 40)); /**Frame Properties**/ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setPreferredSize(new Dimension(dimension1, dimension2)); this.setResizable(false); this.pack(); this.setVisible(true);

了解GridBagLayout约束

我是一个相对较新的摇摆,我试图弄清楚它是如何工作的。 告诉我,如果我错了。 我主要想出了放置组件所必需的gridx和gridy。 您至少需要n个值(gridx,gridy)才能创建nxn网格。 例如(5,5),(3,3),(4,9),(3,10)将创建一个3×4网格空间(4行,3列),其中组件分别使用上面的(gridx,gridy)放置在细胞(3,2),(1,2),(2,3),(1,4)中。 weightx和weighty似乎有两个函数,一个> 0的weightx值和weighty将网格单元拉伸到边缘(否则它是集中的)我们也可以设置比例与组件的大小 – 所以如果一个组件有gridx = 0.1和anothr有0.2,然后后者可能是两倍宽。 但是,在按比例分量时,会考虑组件的最小默认宽度和高度。 需要填充以将组件拉伸到单元格的边缘。 如果没有填充,组件将保持在中心位置。 但是我们可以在这种情况下使用锚来定位组件沿着单元格的西北角而不是中心。 插图在单元格边缘内创建一个空间墙。 ipadx和ipady推动包含单元格的列或行的边界。 但我无法弄清楚网格宽度和网格高度是如何工作的。 考虑下面的这个例子。这4个按钮放在4×4网格空间的单元格b1(3,2),b2(1,1),b3(2,3),b4(4,4)中。 如何使按钮(例如b2或b4)占据它占据的单元格的整个行或列? import javax.swing.*; import java.awt.*; //import java.awt.event.*; public class Swing29a { public static void main(String[] args) { JFrame f1= new JFrame(“GridBag Layout Test”); f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f1.setResizable(true); f1.setLocation(200,100); f1.setSize(800,800); JPanel p1 = new JPanel(); p1.setBackground(Color.black); f1.add(p1); JButton b1= […]

如何使JLabel从下一行开始

JPanel pMeasure = new JPanel(); …. JLabel economy = new JLabel(“Economy”); JLabel regularity = new JLabel(“Regularity”); pMeasure.add(economy); pMeasure.add(regularity); … 当我运行上面的代码时,我得到这个输出: Economy Regularity 如何获得此输出,每个JLabel在新行上启动? 谢谢 Economy Regularity

使用GridBagLayout在菜单栏下方定位按钮

我想将下面的两个按钮添加到JMenuBar 。 我在GUI的整个窗口使用根JPanel ,我添加了不同的组件,到目前为止,两个,菜单栏和包含两个按钮的面板。 但我不知道如何将GridBagConstraints放置在包含按钮的面板上,使其位于菜单栏下方。 这是我的代码: JFrame f = new JFrame(“Cliente AEMET”); JPanel panel_raiz = new JPanel();//root JPanel GridBagConstraints gbc = new GridBagConstraints(); public VentanaPrincipal (){ f.setSize(500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel_raiz.setLayout(new GridBagLayout()); //textoInfoMunicipio(); //ventana_aviso(); //descomentar si se quiere ver el JDialog //lista(); menus(); //the JMenuBar botones(); //the buttons //lista_desplegable(); //d.acerca_de(); f.setContentPane(panel_raiz); //here I put all the […]

CardLayout的父容器/面板

如何在CardLayout获取任何面板的“容器”面板。 也就是说,如果面板是另一个“容器”面板中的卡片,那么如何从卡片中引用这个“容器”面板? 这就是我在做的事: – public class LogInPanel extends javax.swing.JPanel implements ActionListener{ /** * Creates new form Panel2 */ private JPanel parentPanel; private CardLayout c1=null; public LogInPanel() { initComponents(); //c1=new CardLayout(); parentPanel=(JPanel)(SwingUtilities.getAncestorOfClass(this.getClass(), this)); c1=(CardLayout)(parentPanel.getLayout()); submitLogin.addActionListener(this); } …

在JFrame中调整JTabbedPane中的JScrollPane大小

我从这篇文章中了解到, 不要在JComponent上设置大小 ,不要在任何组件上设置首选大小,而是让布局管理器为您执行此操作。 我的问题是,我有一个JPanel,我将JTabbedPane放入这样的JFrame中 JFrame frame=new JFrame(); JTabbedPane pane=new JTabbedPane(); pane.addTab(“Tab 1”,new JScrollPane(getJPanel1())); pane.addTab(“Tab 2”,new JScrollPane(getJPanel2())); frame.setContentPane(pane); 现在,在这种情况下,JTabbedPane将采用您添加到其中的最大大小组件的大小。 因此,我的JScrollPane根本没有显示出来。 我需要设置JScrollPane的首选大小,如果我没有设置它,滚动条将不会出现,内容将被削减。 如何使用布局管理器来解决此问题。 我想具体做到这一点: 使JFrame / JTabbedPane / JPanelInTab扩展到屏幕的高度(进入窗口的任务栏),如果选项卡内容将被剪切,则应显示滚动条。 框架的宽度应与JTabbedPane完全一致。 编辑 这是一个MVCE,显示了我想要做的事情。 我根据peeskillet的建议包括了这些变化,但是没有效果 import javax.swing.*; import java.awt.*; public class ScrollPaneTest { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame=new […]

从JPanel(没有JFrame)创建BufferedImage时,我还可以使用布局管理器吗?

我试图从JPanel创建一个BufferedImage,而不使用JFrame。 昨天,我终于能够在这个社区的帮助下看到图像(见下文),但现在我遇到了一些布局问题。 BufferedImage上的所有组件都以0,0开始绘制,而不是遵循布局管理器。 有谁知道这个问题的解决方案? 昨天的问题: 我可以从JPanel创建BufferedImage而无需在JFrame中渲染吗? 在下面的代码中,两个标签将在图像的左上角相互覆盖。 import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class RenderTest { RenderTest() { JPanel panel = new JPanel(); panel.setBackground(Color.RED); panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); Dimension dim = new Dimension(150,50); panel.setSize(dim); panel.setMinimumSize(dim); panel.setMaximumSize(dim); panel.setPreferredSize(dim); JLabel label = new JLabel(“hello”); JLabel label2 […]

BoxLayout拒绝承认JButton的首选大小

我一直致力于一个应该模拟赌博游戏的小项目。 不幸的是,我在使用BoxLayout时遇到了一些奇怪的问题。 据我所知, LayoutManager通常会尊重任何组件的首选大小。 但是,在下面的代码中, BoxLayout没有。 到目前为止,这是我的代码: import java.awt.*; import javax.swing.*; public class Main { public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame(“Suit-Up”); frame.setContentPane(makeGUI()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(900,450); frame.setLocationRelativeTo(null); frame.setResizable(false); frame.setVisible(true); } public static JPanel makeGUI() { JPanel main = new JPanel(); main.setMinimumSize(new Dimension(900,450)); main.setBackground(Color.red); JPanel infoPanel = new JPanel(); infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.LINE_AXIS)); […]

Java在面板中对齐组件

我试图将组件添加到BorderLayout的SOUTH我需要它看起来像这个例子。 ———————————— | | | | | | | | | | | | |_TxtField|Button_____________Label| 所以我需要一个JTextField和一个与左边对齐的JButton,以及一个与右边对齐的标签。 我怎么能做到这一点? 以下是我的代码,我尝试使用嵌套面板执行此操作: import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class BlackjackGUI{ private JFrame frame; private JPanel panel, panelLeft, panelBottomLeft, panelBottomRight; private JButton newGameBtn, dealBtn, hitBtn, standBtn; private JLabel placeBetLbl, playerMoneyLbl; private JLabel playerCard1Lbl, playerCard2Lbl, playerCard3Lbl, playerCard4Lbl, playerCard5Lbl, […]