亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在正確的窗口中創建 GUI

在正確的窗口中創建 GUI

aluckdog 2022-07-27 20:32:06
我正在嘗試創建一個包含 4 個字段的 GUI。網址用戶名密碼陳述在第一次,這些字段應該是空的。稍后,密碼字段旁邊的所有字段都應包含上次的信息。問題:GUI 窗口不應具有標準大小我想達到的目標:當窗口打開時,它應該動態調整到筆記本電腦的屏幕大小,例如中心,屏幕的 20%第四個字段(語句)可能很長,GUI 窗口會自動變得過長。我想達到的目標:對于第四個字段,應將字符串分解為多行。在將字段分解為三行而不是繼續將其分解為第四行之后,一個選項可能是滾動條。到目前為止,我已經找到了一些可以提供幫助的對象。選項窗格面板第四個長字段的JTextArea對于JTextArea,還有滾動窗格.setLineWrap(true) 換行.setWrapStyleWord(true)在一個單詞后換行我的代碼示例:JPanel pane = new JPanel();// adding GridLayoutpane.setLayout(new GridLayout(4,2));// fields are filled just as an example// later they will get substituted by variablesJTextField url = new JTextField ("https:testin.com");JTextField username = new JTextField ("theDude");JTextArea statement = new JTextArea("This statement can becomme very very very long :)");statement.setLineWrap(true);statement.setWrapStyleWord(true);JScrollPane scrollPane = new JScrollPane(statement);pane.add(scrollPane);// add infos to panepane.add(new JLabel ("Enter url: "));pane.add(url);pane.add(new JLabel ("Enter username: "));pane.add(username);pane.add(new JLabel ("Enter password: "));pane.add(new JPasswordField());pane.add(new JLabel ("Enter statement: "));pane.add(statement);//write it to a OK_CANCEL JOptionPaneint option = JOptionPane.showConfirmDialog(null,pane, "Fill all the fields",JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
查看完整描述

1 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

JTextArea您可以設置using的高度(可見行數)setRows()。試試下面的例子。我從你的代碼開始,做了一些改動。


import javax.swing.*;

import java.awt.*;


public class ThreeLinesTextArea

{

  public static void main(String[] args)

  {

    JPanel pane = new JPanel();

    // Change to GridBagLayout

    pane.setLayout(new GridBagLayout());

    JTextField url = new JTextField("https:testin.com");

    JTextField username = new JTextField("theDude");


    JTextArea statement = new JTextArea("This statement can becomme very very very long :)");

    statement.setLineWrap(true);

    statement.setWrapStyleWord(true);

    // Use setRows() to make text area have multiple lines

    statement.setRows(3);

    JScrollPane scrollPane = new JScrollPane(statement);


    //This line is removed. scrollPane is added at the end.

    //pane.add(scrollPane);


    pane.add(new JLabel("Enter url: "),

        new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,

            GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));

    pane.add(url,

        new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,

            GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));

    pane.add(new JLabel("Enter username: "),

        new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,

            GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));

    pane.add(username,

        new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,

            GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));

    pane.add(new JLabel("Enter password: "),

        new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,

            GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));

    pane.add(new JPasswordField(15),

        new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,

            GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));

    pane.add(new JLabel("Enter statement: "),

        new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,

            GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));

    pane.add(scrollPane,

        new GridBagConstraints(1, 3, 1, 1, 1.0, 1.0, GridBagConstraints.WEST,

            GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));


    int option = JOptionPane.showConfirmDialog(null, pane, "Fill all the fields",

        JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

  }

}

輸出:

http://img1.sycdn.imooc.com//62e1308300010dac03410218.jpg

查看完整回答
反對 回復 2022-07-27
  • 1 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號