Я изначально потратил немало времени, пытаясь перевести размер JPanel, когда диалоговое окно будет изменено, захватив нижний правый угол, мышь и перетаскивание. Я думаю, что это трещины, но теперь я не могу заставить кнопки оставаться центрированными. Я включил некоторый урезанный код, и я вытащил другие панели (я использую JLayeredPane, потому что есть 5 панелей, а не только один). Я видел исправление для этого, если бы использовал JLabel в другом месте на StackOverFlow, но пока не смог его взломать. Любая помощь будет оценена по достоинству. Похоже, я должен упустить что-то действительно простое. Это мой первый пост, поэтому я надеюсь, что вы будете иметь какие-либо ошибки форматирования. Спасибо ....Каков наилучший способ держать JButtons в центре JPanel с помощью GroupLayout в JPanel resize
import java.awt.Color;
import java.awt.Component;
import java.util.logging.Logger;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class mock2Dialog extends JDialog {
private static final long serialVersionUID = 1L;
public static int BUTTON_UNKNOWN = -1;
public static int BUTTON_NO = 1;
public static int BUTTON_YES = 0;
public static int DIALOG_TYPE_YES_NO = 1;
int buttonClicked = BUTTON_UNKNOWN;
int dialogType = 1;
Logger logger = null;
public mock2Dialog() {
}
public mock2Dialog(java.awt.Frame parent, boolean modal, String title,
int dialogType, Logger logger) {
super(parent, modal);
this.logger = logger;
this.dialogType = dialogType;
initComponents();
this.setTitle(title);
buttonClicked = BUTTON_UNKNOWN;
getYesButton().setFocusTraversalKeysEnabled(false);
getNoButton().setFocusTraversalKeysEnabled(false);
getYesNoPanel().setVisible(false);
getYesButton().setVisible(false);
getNoButton().setVisible(false);
onOpen(this); //this fakes out the system so I can execute from this file
}//EOM
public void onOpen(Component caller) {
setLocationRelativeTo(caller);
prepareOnOpen();
}//EOM
public void prepareOnOpen() {
getYesNoPanel().setVisible(false);
getYesButton().setVisible(false);
getNoButton().setVisible(false);
if (dialogType == DIALOG_TYPE_YES_NO) {
getYesNoPanel().setVisible(true);
getYesButton().setVisible(true);
getNoButton().setVisible(true);
}
buttonClicked = BUTTON_UNKNOWN;
if (dialogType == DIALOG_TYPE_YES_NO) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
yesButton.requestFocusInWindow();
}
});
}
this.setVisible(true);
} //EOM
private void initComponents() {
msgScrollPane = new JScrollPane();
msgTextArea = new JTextArea();
jLayeredPane1 = new JLayeredPane(); //no LayoutManager specified by design of component
yesNoPanel = new JPanel();
yesButton = new JButton();
noButton = new JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Message");
//msgScrollPane
msgScrollPane.setBorder(null);
//msgTextArea
msgTextArea.setColumns(20);
msgTextArea.setEditable(false);
msgTextArea.setFont(new java.awt.Font("Verdana", 0, 14));
msgTextArea.setRows(5);
msgTextArea.setWrapStyleWord(true);
msgTextArea.setBorder(javax.swing.BorderFactory.createEtchedBorder());
msgTextArea.setOpaque(false);
msgScrollPane.setViewportView(msgTextArea);
//YesNoPanel
yesNoPanel.setBackground(Color.BLUE);
yesButton.setText("Yes");
yesButton.setName("yesButton");
//yesButton.setAlignmentX(CENTER_ALIGNMENT); //thought this might work to center the button within the panel
noButton.setText("No");
noButton.setName("noButton");
//noButton.setAlignmentX(CENTER_ALIGNMENT); //thought this might work to center the button within the panel
GroupLayout yesNoPanelLayout = new GroupLayout(
yesNoPanel);
yesNoPanel.setLayout(yesNoPanelLayout);
yesNoPanelLayout
.setHorizontalGroup(yesNoPanelLayout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING) //Center doesn't seem to change behaviour
.addGroup(
yesNoPanelLayout
.createSequentialGroup()
.addGap(146, 146, 146)
.addComponent(yesButton)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(noButton)
.addContainerGap(147, Short.MAX_VALUE)));
yesNoPanelLayout
.setVerticalGroup(yesNoPanelLayout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
yesNoPanelLayout
.createSequentialGroup()
.addContainerGap()
.addGroup(
yesNoPanelLayout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(yesButton)
.addComponent(noButton))
.addContainerGap(
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)));
yesNoPanel.setBounds(0, 0, Short.MAX_VALUE, 50); //setting Short.MAX_VALUE will allow the panel to expand.
jLayeredPane1.add(yesNoPanel, javax.swing.JLayeredPane.DEFAULT_LAYER);
//main layout for the dialog
GroupLayout layout = new GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addComponent(jLayeredPane1,
GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, //400,
Short.MAX_VALUE)
.addGap(0, 0, 0))
.addGroup(
layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(msgScrollPane,
GroupLayout.DEFAULT_SIZE,
376,
Short.MAX_VALUE).addContainerGap()));
layout.setVerticalGroup(layout
.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(
GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap()
.addComponent(msgScrollPane,
GroupLayout.DEFAULT_SIZE,
106, Short.MAX_VALUE)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLayeredPane1,
GroupLayout.PREFERRED_SIZE,
50,
GroupLayout.PREFERRED_SIZE)));
pack();
}
private JLayeredPane jLayeredPane1;
private JScrollPane msgScrollPane;
private JTextArea msgTextArea;
private JButton noButton;
private JButton yesButton;
private JPanel yesNoPanel;
public javax.swing.JScrollPane getMsgScrollPane() {
return msgScrollPane;
}
public void setMsgScrollPane(javax.swing.JScrollPane msgScrollPane) {
this.msgScrollPane = msgScrollPane;
}
public javax.swing.JTextArea getMsgTextArea() {
return msgTextArea;
}
public void setMsgTextArea(javax.swing.JTextArea msgTextArea) {
this.msgTextArea = msgTextArea;
}
public String getMsgText() {
return this.msgTextArea.getText();
}
public void setMsgText(String msgText) {
this.msgTextArea.setText(msgText);
msgTextArea.setCaretPosition(0);
msgScrollPane.getVerticalScrollBar().setValue(0);
}
public javax.swing.JButton getNoButton() {
return noButton;
}
public void setNoButton(javax.swing.JButton noButton) {
this.noButton = noButton;
}
public javax.swing.JButton getYesButton() {
return yesButton;
}
public void setYesButton(javax.swing.JButton yesButton) {
this.yesButton = yesButton;
}
public javax.swing.JPanel getYesNoPanel() {
return yesNoPanel;
}
public void setYesNoPanel(javax.swing.JPanel yesNoPanel) {
this.yesNoPanel = yesNoPanel;
}
public static void main(String args[]) {
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Logger logger = null;
int dialogType = 1;
String title = "Mock Dialog";
mock2Dialog dialog = new mock2Dialog(
new javax.swing.JFrame(), false, title, dialogType,
logger);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
}); //nested a method in another methods arguments
dialog.setVisible(true);
dialog.msgTextArea
.setText(" Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sit amet fringilla nunc.\n "
+ "Duis sem nulla, egestas vel elit vitae, pulvinar semper nunc. Nam in nisi quis turpis pellentesque pulvinar. \n"
+ "Nulla facilisi. Pellentesque ac rhoncus ante. Mauris ut magna nibh. Ut eget dapibus diam, sed iaculis erat. "
+ "Vestibulum faucibus neque nisl, non imperdiet libero elementum sed. Fusce molestie eros id ligula consectetur ultrices.");
}
});
}
}
Не используйте GroupLayout. GroupLayout обычно используется только инструментами разработки, которые генерируют код, потому что макет трудно использовать, когда он написан вручную. Для достижения желаемого результата используйте комбинацию других менеджеров компоновки на вложенных панелях. – camickr
После того, как все исследования и встречи с народом будут отвечать на другие вопросы, я определенно соглашусь с вами. Однако это небольшая часть гораздо большего приложения, которое было создано с помощью инструментов разработки, и я думал, что для обеспечения непрерывности лучше работать в рамках этой системы GroupLayout. Это звучит так, как будто может быть не решение (хорошее или иное) к проблеме с использованием GroupLayout, хотя ... Спасибо за ответ! – Godaigo