具有固定高度和可变宽度的 JButton 布局

发布于 2024-12-05 22:50:00 字数 1469 浏览 0 评论 0原文

我寻找一个具有固定高度 JButton 的 LayoutManager,这些 JButton 可以扩展宽度以适应其容器的大小。在 JButton 上方,行上应该有一个 JLabel 本身,上面写着“选择文件:”。它旨在作为 JFileChooser 的附件,让用户选择最近的文件。我一直无法让它看起来很正确,我尝试过使用多个 JPanel 和 LayoutManager,例如 BoxLayout。当使用 BoxLayout 时,JButton 仅扩展至必须包含其文本的范围;但我希望所有的 JButton 具有相同的宽度,这样它们看起来就不会很有趣。

注意:我还使用了其他 LayoutManager,例如 Border 和 GridLayout,但它们大多忽略我的尺寸设置,并且看起来不够复杂。我必须手动完成此操作,Netbeans 等不是一个选择。

工作示例,但视觉上不正确

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Chooser extends JPanel {


    public Chooser(){
        this.setLayout(new GridLayout(0,1));

        JPanel labelPanel = new JPanel();
        JLabel label = new JLabel("Choose a file:");
        labelPanel.add(label);
        labelPanel.setBackground(Color.red);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton("long pathname to a file goes here"));
        buttonPanel.setBackground(Color.blue);

        this.add(labelPanel);
        this.add(buttonPanel);
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Chooser c = new Chooser();
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setAccessory(c);
        fileChooser.showOpenDialog(null);
    }

}

I look for a LayoutManager that has fixed height JButtons that expand in width to fit the size of their Container. Above the JButtons there should be a JLabel by itself on the line that says "Choose file:". This is intended to work as an accessory to a JFileChooser that lets the user choose recent files. I haven't been able to make it look quite right, I've tried using multiple JPanels and LayoutManagers such as BoxLayout. When using BoxLayout the JButtons only expand as far as they have to to contain their text; but I would like for all of the JButtons to be the same width so they don't look funny.

Note: I've also used other LayoutManagers such as Border and GridLayout but those mostly ignore my size settings and aren't sophisticated enough it would seem. I have to do this by hand, Netbeans etc are not an option.

Working example, but visually incorrect

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Chooser extends JPanel {


    public Chooser(){
        this.setLayout(new GridLayout(0,1));

        JPanel labelPanel = new JPanel();
        JLabel label = new JLabel("Choose a file:");
        labelPanel.add(label);
        labelPanel.setBackground(Color.red);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton("long pathname to a file goes here"));
        buttonPanel.setBackground(Color.blue);

        this.add(labelPanel);
        this.add(buttonPanel);
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Chooser c = new Chooser();
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setAccessory(c);
        fileChooser.showOpenDialog(null);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心清如水 2024-12-12 22:50:00

GridLayout 嵌套在 BorderLayout 中(实际上是 JScrollPane 中)的情况怎么样?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Chooser extends JPanel {
   private static final String[] BUTTON_TEXTS = { "Hello", "Goodbye",
         "Hello Goodbye", "Adios", "This is a long String for WTF" };

   public Chooser() {
      this.setLayout(new BorderLayout());

      JPanel labelPanel = new JPanel();
      JLabel label = new JLabel("Choose a file:");
      labelPanel.add(label);
      labelPanel.setBackground(Color.red);

      JPanel buttonPanel = new JPanel(new GridLayout(0, 1));
      for (int i = 0; i < BUTTON_TEXTS.length; i++) {
         buttonPanel.add(new JButton(BUTTON_TEXTS[i]));
      }
      buttonPanel.add(Box.createVerticalGlue()); // to pad the bottom if needed
      buttonPanel.setBackground(Color.blue);

      this.add(labelPanel, BorderLayout.PAGE_START);
      this.add(new JScrollPane(buttonPanel), BorderLayout.CENTER);
   }

   public static void main(String[] args) {
      Chooser c = new Chooser();
      JFileChooser fileChooser = new JFileChooser();
      fileChooser.setAccessory(c);
      fileChooser.showOpenDialog(null);
   }

}

示例 2 使用一个简单的 JList,将其选择放在文件选择器中:

import java.awt.*;
import java.io.File;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Chooser extends JPanel {
   private static final String[] BUTTON_TEXTS = { "Hello", "Goodbye",
         "Hello Goodbye", "Adios", "This is a long String for WTF", "Hello",
         "Goodbye", "Hello Goodbye", "Adios", "This is a long String for WTF" };

   public Chooser(final JFileChooser fileChooser) {
      setLayout(new BorderLayout());

      JPanel labelPanel = new JPanel();
      JLabel label = new JLabel("Choose a file:");
      labelPanel.add(label);
      labelPanel.setBackground(Color.red);

      final JList list = new JList(BUTTON_TEXTS);
      list.addListSelectionListener(new ListSelectionListener() {
         public void valueChanged(ListSelectionEvent e) {
            String selectedString = list.getSelectedValue().toString();
            fileChooser.setSelectedFile(new File(selectedString));
         }
      });

      add(labelPanel, BorderLayout.PAGE_START);
      add(new JScrollPane(list), BorderLayout.CENTER);
   }

   public static void main(String[] args) {
      JFileChooser fileChooser = new JFileChooser();
      Chooser c = new Chooser(fileChooser);
      fileChooser.setAccessory(c);
      fileChooser.showOpenDialog(null);
   }

}

What about something where a GridLayout is nested in a BorderLayout (actually in a JScrollPane)...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Chooser extends JPanel {
   private static final String[] BUTTON_TEXTS = { "Hello", "Goodbye",
         "Hello Goodbye", "Adios", "This is a long String for WTF" };

   public Chooser() {
      this.setLayout(new BorderLayout());

      JPanel labelPanel = new JPanel();
      JLabel label = new JLabel("Choose a file:");
      labelPanel.add(label);
      labelPanel.setBackground(Color.red);

      JPanel buttonPanel = new JPanel(new GridLayout(0, 1));
      for (int i = 0; i < BUTTON_TEXTS.length; i++) {
         buttonPanel.add(new JButton(BUTTON_TEXTS[i]));
      }
      buttonPanel.add(Box.createVerticalGlue()); // to pad the bottom if needed
      buttonPanel.setBackground(Color.blue);

      this.add(labelPanel, BorderLayout.PAGE_START);
      this.add(new JScrollPane(buttonPanel), BorderLayout.CENTER);
   }

   public static void main(String[] args) {
      Chooser c = new Chooser();
      JFileChooser fileChooser = new JFileChooser();
      fileChooser.setAccessory(c);
      fileChooser.showOpenDialog(null);
   }

}

Example 2 with a simple JList that places its selection in the file chooser:

import java.awt.*;
import java.io.File;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Chooser extends JPanel {
   private static final String[] BUTTON_TEXTS = { "Hello", "Goodbye",
         "Hello Goodbye", "Adios", "This is a long String for WTF", "Hello",
         "Goodbye", "Hello Goodbye", "Adios", "This is a long String for WTF" };

   public Chooser(final JFileChooser fileChooser) {
      setLayout(new BorderLayout());

      JPanel labelPanel = new JPanel();
      JLabel label = new JLabel("Choose a file:");
      labelPanel.add(label);
      labelPanel.setBackground(Color.red);

      final JList list = new JList(BUTTON_TEXTS);
      list.addListSelectionListener(new ListSelectionListener() {
         public void valueChanged(ListSelectionEvent e) {
            String selectedString = list.getSelectedValue().toString();
            fileChooser.setSelectedFile(new File(selectedString));
         }
      });

      add(labelPanel, BorderLayout.PAGE_START);
      add(new JScrollPane(list), BorderLayout.CENTER);
   }

   public static void main(String[] args) {
      JFileChooser fileChooser = new JFileChooser();
      Chooser c = new Chooser(fileChooser);
      fileChooser.setAccessory(c);
      fileChooser.showOpenDialog(null);
   }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文