gpt4 book ai didi

java - 在 AWT 中向框架添加滚动条/滚动 Pane

转载 作者:行者123 更新时间:2023-12-02 07:02:11 27 4
gpt4 key购买 nike

您好,我是一名工程专业的大四学生,虽然我学过java,但我对其应用的了解还很初级。但是,我的项目必须使用java。这是我编写的前端代码:

    import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;


public class ex extends Applet
{
static Frame f;
DrawCanvas can;
SetControls cont;

public void init()
{
can=new DrawCanvas();
setLayout(new BorderLayout());
add("Center", can);
add("North", cont = new SetControls(can, f));
}

public void destroy()
{
remove(cont);
remove(can);
}

public void start()
{
cont.setEnabled(true);
}

public void stop()
{
cont.setEnabled(false);
}

public void processWindowEvent(AWTEvent e)
{
if (e.getID() == Event.WINDOW_DESTROY)
{
System.exit(0);
}
}

public static void main(String args[])
{
f = new Frame("Title");
ex fe = new ex();

fe.init();
fe.start();

f.add("Center", fe);
f.setSize(1500,1500);

f.show();
}
}

class DrawCanvas extends Canvas
{
//declaration of variables
public DrawCanvas()
{
}

public void paint(Graphics g)
{
//various flags are set for functions to be called and what shud
//be drawn when those functions are called

}
}


class SetControls extends Panel implements ActionListener
{
TextArea text1;
TextField txt;
Button load, init1, cluster,exit;
Label head, vig;

//declaration of variables used

FileDialog fc;
String fname;
Frame f;
DrawCanvas canvas;

public SetControls(DrawCanvas can, Frame f)
{
//initialization of variables used

canvas = can;
this.f = f;

head = new Label("Clustering of Genes:");
cluster = new Button("Cluster Now");
load = new Button("Load");
init1 = new Button("Filter");
exit = new Button("Exit");
txt = new TextField(2);
text1 = new TextArea(5,80);
vig = new Label("Parameter");


load.addActionListener(this);
init1.addActionListener(this);
txt.addActionListener(this);
cluster.addActionListener(this);
exit.addActionListener(this);


add(head);
add(load);
add(init1);
add(text1);
add(vig);
add(txt);
add(cluster);
add(exit);

}

public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
System.out.println("Command: " + str);

if(str.equals("Load"))
{
text1.setText("Loading microarray data file...");

fc = new FileDialog(f, "Choose a file", FileDialog.LOAD);
fc.setDirectory(".");
fc.setVisible(true);
fname = fc.getFile();
//function call to read the file
text1.setText("Input Data File Loaded: " + fname);
}
else if((str.equals("Filter")||str.equals("Cluster Now")))
{
if(str.equals("Filter"))
{
//function calls and setting of text are
}
else
{
//function calls and setting of text area
}

}


else{
//call close function when exit is pressed
}
}
}

我知道您在想为什么我使用 AWT 而不是 swing,但目前我对 AWT 更满意。到目前为止,代码运行良好。但问题是,当我按下其中一个按钮时,显示的信息很多,超出了框架;所以我想添加一个滚动条或滚动 Pane ,以便我可以向下滚动以查看其余信息。我已经尝试了很多东西,例如 f.add(new ScrollPane()) 等,但它们都不起作用。请帮我!

最佳答案

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Scrollbar;

public class AWTScrollbar {

public static void main(String[] args) {

Frame frame = new Frame("Scrollbar");

FlowLayout layout = new FlowLayout();

frame.setLayout(layout);

Label label = new Label("Scrollbar");

frame.add(label);

Scrollbar scrollbar = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);

frame.add(scrollbar);

frame.setVisible(true);

frame.pack();

}

}

也许这对你有用:)

关于java - 在 AWT 中向框架添加滚动条/滚动 Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16509409/

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