【问题标题】:JLabel on JPanel poitioningJLabel 上 JPanel 定位
【发布时间】:2023-03-12 21:25:01
【问题描述】:

几天来,我一直在尝试让一些 JLabels 在 JPanel 上对行进行编号,但没有任何乐趣。我已经尝试了所有我能想到的布局管理器。目前,我使用 GridLayout 获得了最好的结果(这不是我想要的)。请帮忙!

有问题的领域是 AddLabels() 方法。

问题:如何让下面代码中的 JLabels 与每一行对齐?

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.*;
import java.util.List;

@SuppressWarnings("serial")
public class DrawPanelMain extends JPanel {

    private static final int PREF_W = 1200;
    private static final int PREF_H = 700;

    //Points for Ellipse2D
    private List<Point> POINT_LIST = Arrays.asList(
            new Point(60, 40),
            new Point(60, 100),
            new Point(60, 160),
            new Point(60, 220),
            new Point(60, 280),
            new Point(60, 340),
            new Point(60, 400),
            new Point(60, 460),
            new Point(60, 520),
            new Point(60, 580),
            new Point(120, 100),
            new Point(120, 160),
            new Point(120, 220),
            new Point(120, 280),
            new Point(120, 340),
            new Point(120, 400),
            new Point(120, 460),
            new Point(120, 520),
            new Point(120, 580),
            new Point(180, 160),
            new Point(180, 220),
            new Point(180, 280),
            new Point(180, 340),
            new Point(180, 400),
            new Point(180, 460),
            new Point(180, 520),
            new Point(180, 580),
            new Point(240, 220),
            new Point(240, 280),
            new Point(240, 340),
            new Point(240, 400),
            new Point(240, 460),
            new Point(240, 520),
            new Point(240, 580),
            new Point(300, 280),
            new Point(300, 340),
            new Point(300, 400),
            new Point(300, 460),
            new Point(300, 520),
            new Point(300, 580),
            new Point(360, 340),
            new Point(360, 400),
            new Point(360, 460),
            new Point(360, 520),
            new Point(360, 580),
            new Point(420, 400),
            new Point(420, 460),
            new Point(420, 520),
            new Point(420, 580),
            new Point(480, 460),
            new Point(480, 520),
            new Point(480, 580),
            new Point(540, 520),
            new Point(540, 580),
            new Point(600, 580));

    //Points for labels
    private List<Point> POINT_LIST2 = Arrays.asList(
            new Point (20, 40),
            new Point (20, 100),
            new Point (20, 160));

    private JTabbedPane tabbedPane = new JTabbedPane();
    private int tabIndex = 0;

    public DrawPanelMain() {
        JPanel btnPanel = new JPanel();
        JPanel infoPanel = new JPanel();
        btnPanel.add(new JButton(new AddSwitchAction("Add Switch Panel")));
        btnPanel.add(new JButton(new PushConfigAction("Push Config")));
        btnPanel.add(new JButton(new ActivateAllAction("Activate All")));
        infoPanel.add(new JTextField(20));

        setLayout(new BorderLayout());
        add(tabbedPane, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.PAGE_END);
        add(infoPanel, BorderLayout.EAST);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class AddSwitchAction extends AbstractAction {
            public AddSwitchAction(String name) {
                super(name);
                int mnemonic = (int) name.charAt(0);
                putValue(MNEMONIC_KEY, mnemonic);
            }

        @Override
        public void actionPerformed(ActionEvent e) {
            tabIndex++;
            String title = "Switch " + tabIndex;
            DrawPanel2 tabComponent = new DrawPanel2(POINT_LIST);
            DrawLabels tabComponent2 = new DrawLabels(POINT_LIST2);
            tabbedPane.add(title, tabComponent);
        }
    }

    private class PushConfigAction extends AbstractAction {
        public PushConfigAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(0);
            putValue(MNEMONIC_KEY, mnemonic);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            /*Add code sending the configuration to the switch panel*/
            JOptionPane.showMessageDialog(DrawPanelMain.this, "Configuration Pushed to Panel");
        }
    }

    private class ActivateAllAction extends AbstractAction {
        public ActivateAllAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(1);
            putValue(MNEMONIC_KEY, mnemonic);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            Component comp = tabbedPane.getSelectedComponent();
            if (comp instanceof DrawPanel2) {
                DrawPanel2 drawPanel = (DrawPanel2) comp;
                drawPanel.activateAll();
            }
        }
    }

    private static void createAndShowGui() {
        DrawPanelMain mainPanel = new DrawPanelMain();
        final double version = 0.1;
        JFrame frame = new JFrame("RF Connection Panel " + version);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

}

@SuppressWarnings("serial")
class DrawPanel2 extends JPanel {
    private static final int OVAL_WIDTH = 30;
    private static final Color INACTIVE_COLOR = Color.RED;
    private static final Color ACTIVE_COLOR = Color.green;
    private List<Point> points;
    private List<Ellipse2D> ellipses = new ArrayList<>();
    private Map<Ellipse2D, Color> ellipseColorMap = new HashMap<>();

    public DrawPanel2(List<Point> points) {
        this.points = points;
        for (Point p : points) {
            int x = p.x - OVAL_WIDTH / 2;
            int y = p.y - OVAL_WIDTH / 2;
            int w = OVAL_WIDTH;
            int h = OVAL_WIDTH;
            Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h);
            ellipses.add(ellipse);
            ellipseColorMap.put(ellipse, INACTIVE_COLOR);
        }

        MyMouseAdapter mListener = new MyMouseAdapter();
        addMouseListener(mListener);
        addMouseMotionListener(mListener);
        setLayout(new GridLayout(12, 4, 0, 0));
        setBorder(new EmptyBorder(10, 10, 0, 0));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        for (Ellipse2D ellipse : ellipses) {
            g2.setColor(ellipseColorMap.get(ellipse));
            g2.fill(ellipse);
        }
    }

    private class MyMouseAdapter extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            for (Ellipse2D ellipse : ellipses) {
                if (ellipse.contains(e.getPoint())) {
                    Color c = ellipseColorMap.get(ellipse);
                    c = (c == INACTIVE_COLOR) ? ACTIVE_COLOR : INACTIVE_COLOR;
                    ellipseColorMap.put(ellipse, c);
                }
            }
            repaint();
        }
    }

    public void activateAll() {
        for (Ellipse2D ellipse : ellipses) {
            ellipseColorMap.put(ellipse, ACTIVE_COLOR);
        }
        repaint();
    }
}

@SuppressWarnings("serial")
class DrawLabels extends JPanel {
    private List<Point> points2;
    private List<JLabel> jLabels = new ArrayList<>();
    private int i = 0;

    public DrawLabels(List<Point> points2) {
        this.points2 = points2;
        for (Point p : points2) {
            JLabel jLabel = new JLabel("Row" + i);
            jLabels.add(jLabel);
            i++;
        }
    }
}

【问题讨论】:

  • 一种解决方案:不要使用 JLabels。而是通过paintComponent 绘制标签,就像绘制点一样。
  • @HovercraftFullOfEels 谢谢我会努力的。
  • @HovercraftFullOfEels 在尝试使用您的方法后,我使用最新代码编辑了帖子,它不会填充标签,当我将组件添加到 tabbedPane 时,它​​会生成一个空白选项卡。我错过了什么?

标签: java swing


【解决方案1】:

您遇到的问题是试图消除不同系统之间字体度量的差异,这通常会使标签总是略有不同。即使使用布局管理器,也很难将其作为单个工作单元来完成,而是...

你可以...

创建一个自定义JPanel,其唯一职责是绘制单行点。然后,您可以使用 GridLayoutGridBagLayout 之类的东西将 JLabel 添加到一行中

你可以...

使用Graphics#drawString将行号直接绘制到带有点的面板上

【讨论】:

  • 谢谢,又一个新概念让我看看。感谢您的反馈。
【解决方案2】:

只需为每一行添加另一个JPanel

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel , BoxLayout.Y_AXIS));

for(JLabel[] line : labels){
    JPanel lp = new JPanel();
    lp.setLayout(new BoxLayout(lp , BoxLayout.X_AXIS));
    mainPanel.add(lp);

    for(JLabel label : line)
        lp.add(label);
}

通过这种方式,您可以组合多个布局管理器来满足您的要求。或者创建自己的LayoutManager (http://docs.oracle.com/javase/tutorial/uiswing/layout/custom.html)

【讨论】:

  • 谢谢,我会尝试一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
  • 2019-07-08
相关资源
最近更新 更多