我正在开发一个swing UI,当工具全屏显示或有人手动调整大小时我需要更改textArea的大小..
我正在使用netbeans GUI构建器,所以显然一切都在GroupLayout中。 我当前的GUI只允许说文本区域,我还没有添加其他组件。所以我们有JFrame>> JPanel1>> jTextArea,它固定在顶部和两侧,因此当我调整工具大小时,它会水平扩展以填充整个空间,但因为它没有锚定在底部所以在调整大小时它不会填充底部空间,我被遗漏在底部的空白区域..
我一直在尝试
jPanel1.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
}
});
我在这里要做的是,如果我们调整大小时JPanel1的大小会发生变化,那么它应该被调用并做一些会垂直改变textArea大小的东西..
这就是为了给你提问的基础。现在我真正拥有的是 JPANEL1。在顶部,我将这个TextArea水平地固定在两侧,在它下面我有一个小的JPanel2从一侧水平锚定到另一侧以显示一些数据。 当我全屏显示时,JPanel2和textArea都会调整大小,但是垂直方向会在textArea和JPanel2之间出现一些空白。所以我想的是在textArea底部和Jpanel2顶部之间获得该空间的高度(这也是我不知道该怎么做。)并且在上面提到的方法中我会做一些事情来增加textArea的大小我找到了空白高度。
这是我的代码:
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jPanel2 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("MyBorder"));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 30, Short.MAX_VALUE)
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(88, 88, 88)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration//GEN-END:variables
}
但我不知道该怎么做。 我知道还有其他一些线索,所以请不要将其标记为重复。 如果有任何其他解决方案,请帮我找到它..
我已经通过其他链接以不同方式显示了此问题的解决方案。但是由于以下限制,我无法使用它:
1.我正在使用NetBeans的GUI Builder,它不允许我修改当我在Panel上放置元素时生成的代码内容,所有代码的部分都是自动生成的,因此无法修改。
2.我经历过的解决方案,都有一个共同点,它们没有任何自动生成的代码。他们从零开始建造它。如果我从头开始做,我就可以做到,但事实并非如此。
我希望解决方案的确切情况我们无法修改生成的代码,但调整大小可以动态完成,以便我可以调用一些侦听器并更改大小..
请耐心等待我,我知道这种限制会让像你这样的人在极端情况下提供帮助。
答案 0 :(得分:0)
public class Example extends JPanel {
public Example() {
super(new BorderLayout());
add(new JScrollPane(new JTextArea()), BorderLayout.CENTER);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setContentPane(new Example());
frame.setSize(200, 200);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
编辑:使用grouplayout
public class Example extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater( () -> {
Example frame = new Example ();
frame.setVisible(true);
} );
}
public Example () {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
setContentPane(contentPane);
JPanel panel = new JPanel();
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder("JPanel 2"));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(panel))
.addComponent(panel_1)
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(panel)
.addComponent(panel_1))
);
panel.setLayout(new BorderLayout(0, 0));
JTextArea textArea = new JTextArea();
panel.add(textArea);
contentPane.setLayout(gl_contentPane);
}
}