- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了两个java类,其中一个是我的主类main.java
,另一个是支持类,它创建一个带有一些添加组件CPanel.java
的面板。我的应用程序的布局如下:
Main 类使用 BoxLayout 创建一个主面板,并将工具栏添加到顶部,然后创建一个 CPanel 对象(添加了组件的面板)并将其添加到主面板。 CPanel 对象还具有其组件的 BoxLayout。问题是,CPanel 面板没有合并 BoxLayout,而是只坚持流布局。这是我的类(class)的代码..
public class MainFile extends JFrame {
private JToolBar navbar ;
private JButton backBtn, forwardBtn, homeBtn ;
private CPanel content ;
private static JPanel app = new JPanel() ;
public MainFile(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
navbar = new JToolBar() ;
navbar.setMaximumSize(new Dimension(1000, 50));
setSize(500, 800) ;
app.setLayout(new BoxLayout(app, BoxLayout.PAGE_AXIS));
app.add(navbar , BorderLayout.NORTH ) ;
ImageIcon leftButtonIcon = createImageIcon("images/right.gif");
ImageIcon middleButtonIcon = createImageIcon("images/middle.gif");
ImageIcon rightButtonIcon = createImageIcon("images/left.gif");
backBtn = new JButton(leftButtonIcon) ;
forwardBtn = new JButton(rightButtonIcon) ;
homeBtn = new JButton(middleButtonIcon) ;
navbar.add(forwardBtn) ;
navbar.add(Box.createGlue());
navbar.add(homeBtn) ;
navbar.add(Box.createGlue());
navbar.add(backBtn) ;
content = new CPanel() ;
app.add(content) ;
setContentPane(app) ;
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = MFrame.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public static void showGUI(){
MFrame Reader = new MFrame() ;
//Reader.pack();
//Reader.setContentPane(app);
Reader.setVisible(true) ;
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
showGUI() ;
}
});
}
}
这是我的 CPanel 类
public class CPanel extends JPanel {
private JScrollPane scrollPane ;
private JPanel content ;
private JEditorPane demo ;
public CPanel(){
scrollPane = new JScrollPane() ;
content = new JPanel() ;
scrollPane.setViewportView(content);
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
try{
java.net.URL text = CPanel.class.getResource("demo.txt") ;
demo = new JEditorPane(text) ;
}catch(Exception e){
System.out.println("Something bad happened with the file") ;
}
add(demo) ;
JButton demob = new JButton("Button 1") ;
JButton demob2 = new JButton("Button 2") ;
add(demob) ;
add(demob2) ;
}
}
最佳答案
您似乎希望 CPanel
有一个 JScrollPane
,它作为 BoxLayout
的组件出现。您创建了 JPanel
、设置其布局并将其添加到 JScrollPane
中是正确的,但您仍然需要将 JScrollPane
添加到您的 CPanel
以及按钮和 demo
到 content
public class CPanel extends JPanel {
private JScrollPane scrollPane ;
private JPanel content ;
private JEditorPane demo ;
public CPanel(){
scrollPane = new JScrollPane() ;
content = new JPanel() ;
scrollPane.setViewportView(content);
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
try{
java.net.URL text = CPanel.class.getResource("demo.txt") ;
demo = new JEditorPane(text) ;
}catch(Exception e){
System.out.println("Something bad happened with the file") ;
}
//These need to be added to the contentpanel
content.add(demo) ;
JButton demob = new JButton("Button 1") ;
JButton demob2 = new JButton("Button 2") ;
content.add(demob) ;
content.add(demob2) ;
//Here we need to add the scrollPane, to which the JPanel
//with BoxLayout has been added
this.setLayout(new BorderLayout());
this.add(scrollPane, BorderLayout.CENTER);
}
}
关于java - 嵌套 BoxLayout 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31522853/
以下用于布局组件的代码返回错误 public class MainPanel extends JPanel { private JTextField txt1, txt2; priva
我试图将 4 个水平盒子布局以垂直方式堆叠在 BoxLayout 中。 我的 KV 文件: : BoxLayout: size: root.size pos:
我知道这里有很多 BoxLayout 问题,但是我找不到可以解决我的问题的问题。我需要我的 ScoreDescPanel 来在彼此正下方显示每个标签(如列表),但是我在使用 BoxLayout 时遇到
我正在使用 BoxLayout 来定位服务器 GUI 的 GUI 元素。我的 JLabels 发生了一件奇怪的事情。我想知道为什么当我将字符串附加到 JTextArea 时,JLabels 会从其原始
我正在尝试制作一个简单的计算器,以熟悉我刚刚在计算机科学入门类(class)中学到的 GUI 的创建。我在按照我想要的方式排列 GUI 时遇到问题。我认为 BoxLayout 是完美的,但每当我尝试使
我有一个非常简单的自定义组件。它的首选最大尺寸为 100x100,并且只是一个红色矩形。我将其添加到使用框布局的 JPanel 中。我预计自定义组件的大小为 100x100,但实际上它是 50x100
我刚刚开始摆弄 BoxLayout 管理器。 我制作了两个相邻的按钮,第三个按钮应该转到下一行(前两个按钮下方),前两个按钮应该位于框架的顶部。 我怎样才能做到这一点? 这是我当前的代码 Bo
我的这段代码似乎抛出了 IllegalComponentException,但我不确定为什么。归结为以下几行代码: JRadioButton setRed = new JRadioButton(“Re
查看了一些以前与我的主题相关的帖子,但也无济于事。 尝试使用 BoxLayout 对齐组件但我无法让它发挥作用。我已经修改了一段时间,得到了不同的结果,但我无法弄清楚。我用的是默认的FlowLayou
我有两个 JPanel,我想将它们排列在一个更大的 JPanel 内部,一个叠在另一个之上。 “panel_controls”覆盖.getPreferredSize()。代码: public fina
你好,我正在尝试以与声明 BorderLayout 相同的方式声明 BoxLayout 声明borderlayout的方式 JPanel textFiled = new JPanel(new Bord
如所附屏幕截图所示 - 黄线来自底层 BoxLayout 面向的 JPanel。更改为 BorderLayout 删除黄线: 代码示例如下: import java.awt.BorderLayout;
是的,我用谷歌搜索了大约 30 分钟。是的,stackoverflow 中有 2 个关于该主题的不同帖子,但这些帖子并没有为我的问题提供任何解决方案。 我正在使用相当多的带有 BoxLayout 的面
我正在尝试在我的程序中制作 BoxLayout 布局。我使用 Eclipse,所以当我输入这些行时, JTextArea fntxt = new JTextArea(1, 20); JTextArea
我正在创建一个具有多个聊天的消息传递程序。在聊天窗口的一侧有一个 JPanel,其中包含用于切换聊天的所有按钮的列表数组。我无法使侧面板中的所有按钮具有相同的宽度,无论它们包含什么。但无论我尝试什么似
我似乎无法在网上找到解决方案来解释为什么我在尝试运行时遇到此错误 我正在为不同的程序制作一个简单的测试系统,当按下按钮时将在文本框中产生值。我希望它们位于不同的行以使其更清晰,所以我研究了布局。我认为
我试图弄清楚如何在运行时将项目插入到 BoxLayout...或使用不同的小部件/面板/布局实现类似的效果。 应用程序窗口的设计是垂直堆叠多个面板(BoxLayout.Y_AXIS)。然后,用户可以插
我正在尝试使用 BoxLayout 而不是我的 GridLayout 代码: setLayout (new GridLayout (5, 2)); 除了我注意到,当你做 BoxLayout 时,你需要
谁能帮帮我。为什么标签“当前”没有在面板/框架中左对齐? public static void main(String[] args) { JFrame TFrame =
我正在尝试沿面板中心的垂直轴对齐多个元素,而 BoxLayout 似乎正是我所需要的。但是,当添加的所有元素都具有奇数宽度时,它似乎会做一些奇怪的事情。 这是演示这种古怪行为的 SSCCE: impo
我是一名优秀的程序员,十分优秀!