简体   繁体   English

将 Jbuttons 移动到所需位置

[英]Moving Jbuttons to desired location

I need help with moving buttons to the left.我需要帮助将按钮向左移动。 I want to know how to move buttons to my desired location.我想知道如何将按钮移动到我想要的位置。 I have tried setHorizontalTextPosition() and setBounds() function but not working.我试过 setHorizontalTextPosition() 和 setBounds() function 但没有工作。 Any help or suggestion would be appreciated.任何帮助或建议将不胜感激。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class test {
JFrame frame = new JFrame("test");

    JPanel panelCont = new JPanel();
    JPanel panelFirst = new JPanel();
    JPanel panelSecond = new JPanel();
    JButton buttonOne = new JButton("Sign in");

    JButton Sign_up = new JButton("Sign up");
    JButton buttonSecond = new JButton("log_in");
    CardLayout cl = new CardLayout();

    public test() {
        frame.setPreferredSize(new Dimension(450, 450));
        panelCont.setLayout(cl);


        panelFirst.add(buttonOne);
        panelFirst.add(Sign_up);

        panelSecond.add(buttonSecond);

        panelCont.add(panelFirst, "1");
        panelCont.add(panelSecond, "2");
        cl.show(panelCont, "1");


        buttonOne.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                cl.show(panelCont, "2");
            }
        });

        buttonSecond.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                cl.show(panelCont, "1");
            }
        });

        frame.add(panelCont);
        frame.getContentPane().add(Sign_up, BorderLayout.WEST);
        frame.getContentPane().add(buttonOne, BorderLayout.WEST);

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
}

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new test();
            }
        });
    }
}

My current code gives the output:我当前的代码给出了 output:

What I want my buttons to appear like this:我希望我的按钮看起来像这样:

let do this with tool builder for java swing, it will faster and easier than a lot if you work eclipse, try download windowbuilder from eclipse market,run that low-code platform, chose absolute layout and enjoy. let do this with tool builder for java swing, it will faster and easier than a lot if you work eclipse, try download windowbuilder from eclipse market,run that low-code platform, chose absolute layout and enjoy. Hope it will help you希望它会帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM