gpt4 book ai didi

java - 在 CardLayout 中设置 JTextFields 和 JButtons 位置

转载 作者:行者123 更新时间:2023-12-02 06:09:44 26 4
gpt4 key购买 nike

这是我发现的关于卡片布局的java模板

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Main {

private static final String CARD_JBUTTON = "Card JButton";
private static final String CARD_JTEXTFIELD = "Card JTextField";
private static final String CARD_JRADIOBUTTON = "Card JRadioButton";

private static void createAndShowGUI()
{
JFrame frame = new JFrame("Card Layout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);

// This JPanel is the base for CardLayout for other JPanels.
final JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout(200, 200));

/* Here we be making objects of the Window Series classes
* so that, each one of them can be added to the JPanel
* having CardLayout.
*/
Window1 win1 = new Window1();
contentPane.add(win1, CARD_JBUTTON);
Window2 win2 = new Window2();
contentPane.add(win2, CARD_JTEXTFIELD);
Window3 win3 = new Window3();
contentPane.add(win3, CARD_JRADIOBUTTON);

/* We need two JButtons to go to the next Card
* or come back to the previous Card, as and when
* desired by the User.
*/
JPanel buttonPanel = new JPanel();
final JButton previousButton = new JButton("PREVIOUS");
previousButton.setBackground(Color.BLACK);
previousButton.setForeground(Color.WHITE);
final JButton nextButton = new JButton("NEXT");
nextButton.setBackground(Color.RED);
nextButton.setForeground(Color.WHITE);

buttonPanel.add(previousButton);
buttonPanel.add(nextButton);

/* Adding the ActionListeners to the JButton,
* so that the user can see the next Card or
* come back to the previous Card, as desired.
*/
previousButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.previous(contentPane);
}
});
nextButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});

// Adding the contentPane (JPanel) and buttonPanel to JFrame.
frame.add(contentPane, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.PAGE_END);

frame.pack();
frame.setVisible(true);
}

public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}

这是我的 Window1.java

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;



class Window1 extends JPanel
{
/*
* Here this is our first Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of Two JButtons.
*/
private ActionListener action;

public Window1()
{
init();
}

private void init()
{
final JButton clickButton = new JButton("Click ME");
final JButton dontClickButton = new JButton("DON\'T CLICK ME");

final JTextField title = new JTextField(12);

action = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == clickButton)
{
String myString = title.getText();
System.out.println(myString);
}
else if (ae.getSource() == dontClickButton)
{
JOptionPane.showMessageDialog(null, "I told you not to click me!"
, "Wrong Button", JOptionPane.PLAIN_MESSAGE);
}
}
};

clickButton.addActionListener(action);
dontClickButton.addActionListener(action);

add(clickButton);
add(dontClickButton);
add(title);

}
}

现在我的问题是如何设置 Window1 中文本字段和按钮的位置?

使用此代码,它们被设置在水平对齐的 View 中心。

我尝试使用 title.setLocation(5,5); 但它不起作用。有什么建议吗?

最佳答案

Now my problem is that how do i set the position of the textfields and buttons in Window1? Rows like Jlabel - JTextField then new row ,and in the end of the page the button

问题是您没有使用任何布局管理器。 JPanel 的默认布局管理器是 FlowLayout,它将完全执行您所经历的操作(组件的水平布局)。

可以通过使用不同的布局管理器来实现垂直对齐。您可以对所有组件使用GridBagLayout,或者使用GridLayout,或者可以使用不同的布局嵌套JPanel经理。可能性是无止境。这只是归结为您想要的确切外观。

参见Laying out Components Within a Container了解如何使用不同的布局管理器。我会给你一个例子,但不要让它阻止你查看教程。您需要学习它们。

此外,除了组件的定位之外,布局管理器还使用动态调整大小,要么尊重组件的首选,要么不尊重它们。您可以在 this answer 中看到图片一些布局管理器尊重或不尊重首选尺寸。

enter image description here

import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class LayoutManagers extends JPanel{

public LayoutManagers() {
JLabel label = new JLabel("Text Field");
JTextField textField = new JTextField(20);
JRadioButton rb1 = new JRadioButton("Radio 1");
JRadioButton rb2 = new JRadioButton("Radio 2");
JButton button = new JButton("Button");

JPanel panel1 = new JPanel();
panel1.add(label);
panel1.add(textField);

JPanel panel2 = new JPanel();
panel2.add(rb1);
panel2.add(rb2);

JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.TRAILING));
panel3.add(button);

JPanel panel4 = new JPanel(new GridLayout(3, 1));
panel4.add(panel1);
panel4.add(panel2);
panel4.add(panel3);

setLayout(new GridBagLayout());
add(panel4);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
JFrame frame = new JFrame();
frame.add(new LayoutManagers());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
}

关于java - 在 CardLayout 中设置 JTextFields 和 JButtons 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21990250/

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