Tag: layout manager

为什么这段代码的JTextArea占用了整个JFrame?

我希望我的框架的一部分包含JTextArea但它完全占用。 我无法在这里追踪错误。 import java.awt.*; import javax.swing.*; public class EchoServer { public static void main(String args[]) { CalcFrame c = new CalcFrame(); CalcTextArea a = new CalcTextArea(); } } class CalcTextArea { JTextArea historyDisplayer = new JTextArea(“”,50,20); CalcTextArea() { //historyDisplayer.setVisible(true); historyDisplayer.insert(“Hello World”, 0); Color bg = new Color(23,34,56); historyDisplayer.setBackground(bg); historyDisplayer.setBackground(bg); } } class CalcFrame extends CalcTextArea […]

java GUI中的CardLayout()

在我的代码中,目前每张卡都是我的框架的大小。 如何在布局中设置每个面板的不同尺寸。 我尝试通过调用run()方法并更改框架的大小来使框架具有不同的大小,但它不起作用。 我希望还有另一种方式。 这是我的代码: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class GameManager { JFrame frame; JPanel cards,Title; public GameManager() { cards = new JPanel(new CardLayout()); Title title = new Title(); cards.add(title,”title”); CardLayout cl = (CardLayout)(cards.getLayout()); cl.show(cards, “title”); } public static void main(String [] args) { GameManager gm = new GameManager(); gm.run(); […]

JButton.setBounds(x,y,w,h)似乎不起作用

import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class SimpleExample extends JFrame { public SimpleExample() { setTitle(“Simple example”); setSize(500, 500); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); JButton jb = new JButton(“TEST”); jb.setBorderPainted(true); jb.setBounds(5, 5, 1, 1); —> This line add(jb); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { SimpleExample ex = new SimpleExample(); […]

CardLayout,通过ButtonClick在JPanel之间切换

我想通过点击JPanels上的按钮在JPanel之间切换。 例如:我有一个带有JButton simknop的JPanel sim和带有JButton helpknop的JPanel帮助我想通过单击按钮在这两个JPanel之间切换。 当我点击JButton simknop时,应该出现JPanel帮助,当我点击JButton时,应该会出现JPanel sim。 您可以在下面找到不同的类: main.java public class main extends JFrame { JPanel cards; sim sim; help help; public main() { this.setSize(1024,768); //this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle(“Crazy Bombardement”); this.setLocation(800, 100);//standaard in de hoek van het scherm cards = new JPanel(); cards.setLayout(new CardLayout()); sim = new sim(); help = new help(); cards.add(sim, […]

JButton列之间的间距

我正在开发一个简单的GUI,其中前两列和JButtons的下两列之间有一个小岛。 代码如下: JPanel panel = new JPanel(new GridLayout(50, 4)); JScrollPane scrollable = new JScrollPane(panel); for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { JButton button = new JButton("Row " + row + " seat " + column); panel.add(button); } } 当前外观如何使用java swing在前两列和后两列之间添加一个isle?

带有LayoutManager的JLayeredPane

情况:绘制一堆扑克牌,就像在纸牌游戏中一样。 很好地堆积。 为此,我将JLayeredPane与LayoutManager接口的自定义实现结合使用。 使用自定义LayoutManager的原因是堆栈方向不同,有时候扑克牌完全相互覆盖,有时是部分覆盖,这个逻辑对于LayoutManager来说似乎是一个很好的工作,因为这基本上归结为设置的位置。牌。 因此, LayoutManager负责设置堆栈中所有组件的X坐标和Y坐标。 另一方面, JLayeredPane负责其Z坐标(通过其层)。 将组件添加到JLayeredPane是这样的: JLayeredPane pane = new JLayeredPane(); pane.setLayout(new StackLayout(…)); pane.add(new CardView(…), new Integer(j)); 其中new Integer(j)是卡的一层。 由于JLayeredPane的合同,这必须是Integer 。 这里的问题是,由于上述原因,我的StackLayout不能有任何其他约束对象而不是Integer 。 LayoutManager接口要求您实现以下方法: addLayoutComponent(Component comp, Object constraints); 并且传递的Object将始终为Integer 。 在我的特殊情况下,我很幸运,因为我的XY坐标可以根据Z坐标计算。 例如,层k的卡必须位于Y坐标k * offset 。 所以在我的情况下,约束对象是一个Integer不是问题。 我想知道当Z坐标和XY坐标之间没有相关性时你应该做什么? 你怎么解决这个问题呢? 例如,我如何将GridBagLayout与JLayeredPane结合使用,其中第一个需要GridBagConstraints对象,第二个需要Integer对象? 当然,GBL将以组件不重叠的方式布局,但这只是想法。

如何使JTextArea粘贴到窗口

你好我想让这个TextArea坚持使用鼠标调整窗口大小,就像下按钮一样。 这是完全没有错误的代码,请一目了然。 import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.LayoutManager; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class Rozklady extends JFrame { public Rozklady() { super(); } public void createGUI(){ setPreferredSize(new Dimension(400,150)); JPanel jp = new JPanel(); […]

当需要绝对定位时,null布局管理器的替代解决方案

几个月前,我阅读了这个Swing教程http://zetcode.com/tutorials/javaswingtutorial/resizablecomponent/ ,了解如何在Swing中实现可resize的组件。 本教程讲述了使用null LayoutManager以便有可能绝对定位子组件。 在关于另一个主题的讨论中,@ Andrew Thompson指出使用null布局是个坏主意。 这篇文章http://download.oracle.com/javase/tutorial/uiswing/layout/none.html描述了null布局管理器,因为它无论如何都说在某些情况下它可能是有用的。 在以下情况: 我需要绝对定位JPanel的子组件 我需要为子组件实现我自己的边界框条件(以便建立可能的重叠情况) 我需要JPanel的子组件可resize 在描述的情况下是否正确使用null布局管理器? 或者还有其他更好的解决方案来使用我的JPanel容器组件的布局管理器来实现这些目标? 提前致谢

将JLabel置于JPanels中心

我正在制作一个记分计划,但我遇到了一个问题。 我试图做的是在顶部有一个包含两个JPanel的JPanel,而JPanel又包含两个团队名称。 我很困惑为什么程序顶部的两个JLabel没有集中在它们所包含的JPanel中。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ScoreFrame extends JFrame { private static final Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize(); private static final int WIDTH = SCREEN_SIZE.width; private static final int HEIGHT = SCREEN_SIZE.height; private final JTextField[] nameField = new JTextField[] { new JTextField(), new JTextField() }; private final JLabel[] nameLabel = new […]

适用于可resize的组件的LayoutManager

前段时间我读到这篇文章 ,展示了一种在Swing中实现鼠标可resize的组件的方法。 作者使用null LayoutManager以允许绝对组件定位。 我知道永远不应该使用null布局,所以我的问题是: 是否有任何已经实现的LayoutManager允许组件的绝对定位,或者我必须自己实现它?