gpt4 book ai didi

java - JTextField 剪裁

转载 作者:行者123 更新时间:2023-11-29 05:02:56 27 4
gpt4 key购买 nike

我一直在使用 JPanel,在提供的代码中名为 infoPanel,我希望能够在它前面精确地放置一个带有 JLabel 的 JTextField。我尝试向其中添加 JLabel,但无法将其放置在我想要的位置。我想把它放在比提供的代码低大约 20 个像素的位置,并且在它前面我想添加 JTextField。我还想让 JTextField 只读。

  1. 如何精确放置 JTextField 和 JLabel?

  2. 这方面的最佳实践是什么?

    import javax.swing.*;
    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;
    private List<Point> POINT_LIST = Arrays.asList(new Point[] {
    new Point(40, 40),
    new Point(40, 100),
    new Point(40, 160),
    new Point(40, 220),
    new Point(40, 280),
    new Point(40, 340),
    new Point(40, 400),
    new Point(40, 460),
    new Point(40, 520),
    new Point(40, 580),
    new Point(100, 100),
    new Point(100, 160),
    new Point(100, 220),
    new Point(100, 280),
    new Point(100, 340),
    new Point(100, 400),
    new Point(100, 460),
    new Point(100, 520),
    new Point(100, 580),
    new Point(160, 160),
    new Point(160, 220),
    new Point(160, 280),
    new Point(160, 340),
    new Point(160, 400),
    new Point(160, 460),
    new Point(160, 520),
    new Point(160, 580),
    new Point(220, 220),
    new Point(220, 280),
    new Point(220, 340),
    new Point(220, 400),
    new Point(220, 460),
    new Point(220, 520),
    new Point(220, 580),
    new Point(280, 280),
    new Point(280, 340),
    new Point(280, 400),
    new Point(280, 460),
    new Point(280, 520),
    new Point(280, 580),
    new Point(340, 340),
    new Point(340, 400),
    new Point(340, 460),
    new Point(340, 520),
    new Point(340, 580),
    new Point(400, 400),
    new Point(400, 460),
    new Point(400, 520),
    new Point(400, 580),
    new Point(460, 460),
    new Point(460, 520),
    new Point(460, 580),
    new Point(520, 520),
    new Point(520, 580),
    new Point(580, 580)
    });
    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"))); //Button I want to work with
    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);
    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(new Runnable() {
    public void run() {
    createAndShowGui();
    }
    });
    }

    }

    @SuppressWarnings("serial")
    class DrawPanel2 extends JPanel {
    private static final int OVAL_WIDTH = 40;
    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);
    }

    @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();
    }
    }

最佳答案

How can I precisely place JTextField and JLabels?

不要专注于此,专注于让 UI 的流程对用户来说有意义且简单。

What is the best practice for this?

使用一个或多个合适的布局管理器,在布局管理器支持它们的地方使用 Insets 和/或在它们不支持的地方使用 Border

例如……

Label and field

JPanel fields = new JPanel();
fields.add(new JLabel("Hello: "));
fields.add(new JTextField(20));
fields.setBorder(new EmptyBorder(20, 2, 0, 2));

infoPanel.add(fields);

参见 Laying Out Components Within a ContainerHow to Use Borders了解更多详情

关于java - JTextField 剪裁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31443615/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com