简体   繁体   English

使用击键增加窗口大小

[英]Make a window increase in size using keystrokes

I went with what I could find in java for dummies and what I'm doing isn't working? 我使用了我在Java中可以找到的虚拟对象,而我在做什么却没有用? I'd like a fix and some advice on whether I should focus on simpler things. 我想要一个修复程序以及一些有关是否应该专注于简单事物的建议。

this is the code... 这是代码...

package frame;
import java.util.Scanner;
import javax.swing.JFrame ;

public class Frame_create {`enter code here`
public static void main(String args[]) {
JFrame myFrame = new JFrame() ;
String myTitle = "blank frame" ;
myFrame.setTitle(myTitle) ;
myFrame.setSize(width, height) ;
myFrame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE) ;
myFrame.setVisible(true) ;
}

static int width = 600 ; 
static int height = 200 ;
private Scanner readyread; 



public void input(String args[]){
    readyread = new
    Scanner(System.in);
    int inputNumber = readyread.nextInt();
    int increase = inputNumber*100 ;
    width += increase ;
}}

Are you calling the input(String args[]) method anywhere? 您是否在任何地方调用input(String args[])方法? Below is a modification which creates a Frame_create object and then calls the the input method on it. 下面是一个修改,它创建一个Frame_create对象,然后在其上调用input方法。 The input method is also changed to take in the JFrame object as a second parameter so that the setSize method can be called on it again to set the increased width. input方法也被更改为将JFrame对象作为第二个参数,以便可以再次在其上调用setSize方法以设置增加的宽度。 The number that will denote the increase in size needs to be typed in the console. 需要在控制台中键入表示大小增加的数字。

package frame;
import java.util.Scanner;
import javax.swing.JFrame ;

public class Frame_create {
public static void main(String args[]) {
  Frame_create fc = new Frame_create();
  JFrame myFrame = new JFrame() ;
  String myTitle = "blank frame" ;
  myFrame.setTitle(myTitle) ;
  myFrame.setSize(width, height) ;
  myFrame.setDefaultCloseOperation
      (JFrame.EXIT_ON_CLOSE) ;
  myFrame.setVisible(true) ;
  fc.input(args, myFrame);
}

static int width = 600 ; 
static int height = 200 ;
private Scanner readyread; 

public void input(String args[], JFrame myFrame){
    readyread = new
    Scanner(System.in);
    int inputNumber = readyread.nextInt();
    int increase = inputNumber*100 ;
    width += increase ;
    myFrame.setSize(width, height);
  }
}

This seems to be working. 这似乎正在工作。 Hope this helps. 希望这可以帮助。 Note that if you want to continuously type in a changed size and see the width increase then you will have to put the call to input method in a loop. 请注意,如果要连续键入更改的大小并看到宽度增加,则必须将对input法的调用置于循环中。

While this might answer the question, using console and GUI together in this fashion may not be optimal in a real world application. 尽管这可能会回答问题,但以这种方式同时使用控制台和GUI在现实应用程序中可能不是最佳选择。

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

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