Java JLabel关于JPanel点评

Java JLabel关于JPanel点评,java,swing,Java,Swing,几天来,我一直在尝试让一些jlabel对JPanel上的行进行编号,但毫无乐趣。我已经试过了我能想到的每一个布局经理。目前,我在GridLayout中获得了最好的结果(这不是我想要的)。请帮忙 讨论的区域是AddLabels()方法 问题:如何使下面代码中的JLabel与每行对齐 import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; import java.awt.event.ActionE

几天来,我一直在尝试让一些jlabel对JPanel上的行进行编号,但毫无乐趣。我已经试过了我能想到的每一个布局经理。目前,我在GridLayout中获得了最好的结果(这不是我想要的)。请帮忙

讨论的区域是AddLabels()方法

问题:如何使下面代码中的JLabel与每行对齐

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++;
        }
    }
}
import javax.swing.*;
导入javax.swing.border.EmptyBorder;
导入java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.awt.geom.Ellipse2D;
导入java.util.*;
导入java.util.List;
@抑制警告(“串行”)
公共类DrawPanelMain扩展了JPanel{
专用静态最终整数预值=1200;
专用静态最终整型参数参数H=700;
//Ellipse2D的要点
私有列表点\u List=Arrays.asList(
新点(60,40),
新点(60100),
新点(60,160),
新点(60220),
新点(60280),
新点(60340),
新点(60400),
新点(60460),
新点(60520),
新点(60580),
新点(120100),
新点(120160),
新点(120220),
新点(120280),
新点(120340),
新点(120400),
新点(120460),
新点(120520),
新点(120580),
新点(180160),
新点(180、220),
新点(180280),
新点(180340),
新点(180400),
新点(180460),
新点(180520),
新点(180580),
新点(240220),
新点(240280),
新点(240340),
新点(240400),
新点(240460),
新点(240520),
新点(240580),
新点(300280),
新点(300340),
新点(300400),
新点(300460),
新点(300520),
新点(300580),
新点(360340),
新点(360400),
新点(360460),
新点(360520),
新点(360580),
新点(420400),
新点(420460),
新点(420520),
新点(420580),
新点(480460),
新点(480520),
新点(480580),
新点(540520),
新点(540580),
新点(600580);;
//标签点
私有列表点_LIST2=Arrays.asList(
新观点(20,40),
新点(20100),
新点(20,160));
private JTabbedPane tabbedPane=新JTabbedPane();
私有int tabIndex=0;
公共绘图面板main(){
JPanel btnPanel=新的JPanel();
JPanel infoPanel=新的JPanel();
添加(新的JButton(新的AddSwitchAction(“添加开关面板”));
添加(新的JButton(新的PushConfigAction(“pushConfig”));
添加(新的JButton(新的ActivateLaction(“全部激活”));
infoPanel.add(新JTextField(20));
setLayout(新的BorderLayout());
添加(选项卡窗格,BorderLayout.CENTER);
添加(btnPanel,BorderLayout.PAGE_END);
添加(infoPanel,BorderLayout.EAST);
}
@凌驾
公共维度getPreferredSize(){
如果(isPreferredSizeSet()){
返回super.getPreferredSize();
}
返回新维度(PREF_W,PREF_H);
}
私有类AddSwitchAction扩展了AbstractAction{
公共AddSwitchAction(字符串名称){
超级(姓名);
int助记符=(int)name.charAt(0);
putValue(助记符键,助记符);
}
@凌驾
已执行的公共无效操作(操作事件e){
tabIndex++;
String title=“Switch”+tabIndex;
DrawPanel2 tabComponent=新的DrawPanel2(点列表);
DrawLabels选项卡Component2=新的DrawLabels(点2);
tabbedPane.add(标题,tabComponent);
}
}
私有类PushConfigAction扩展了AbstractAction{
公共PushConfigAction(字符串名称){
超级(姓名);
int助记符=(int)name.charAt(0);
putValue(助记符键,助记符);
}
@凌驾
已执行的公共无效操作(操作事件e){
/*添加将配置发送到开关面板的代码*/
showMessageDialog(DrawPanelMain.this,“配置推送到面板”);
}
}
私有类ActivateLaction扩展了AbstractAction{
公共ActivateLaction(字符串名称){
超级(姓名);
int助记符=(int)name.charAt(1);
putValue(助记符键,助记符);
}
@凌驾
已执行的公共无效操作(操作事件e){
Component comp=tabbedPane.getSelectedComponent();
if(绘图板2的组件实例){
DrawPanel2 drawPanel=(DrawPanel2)公司;
drawPanel.activateAll();
}
}
}
私有静态void createAndShowGui(){
DrawPanelMain mainPanel=新的DrawPanelMain();
最终双版本=0.1;
JFrame框架=新JFrame(“RF连接面板”+版本);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(主面板);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
调用器(()->createAndShowGui());
}
}
@抑制警告(“串行”)
类DrawPanel2扩展了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);
}