0
我正在创建一个框架,它将显示一个显示某些产品位置的Jbuttons列表。我希望这个列表是按钮的原因是如果它们被点击,将它们链接到其他框架。我想知道一种可以通过整个列表的滚动条的方法。我公司目前拥有的代码如下:如何在具有多个按钮的jframe上应用滚动条
enter code here
import javax.swing.*;//window settings
import java.awt.*;//font settings
import java.awt.event.*;//action settings
public class locationFrame extends JFrame implements ActionListener
{
Font tFont=new Font("Arial",Font.BOLD,46);
Font bFont=new Font("Arial",Font.BOLD,52);
JLabel Title=new JLabel("Enid's Picks");
JLabel label=new JLabel("");
Image img=new
ImageIcon(this.getClass().getResource("/HPMLOGO.png")).getImage();
JButton backButton=new JButton("Go Back");
JButton AlemaniButton=new JButton("Alemania");
JButton ArgentinaButton=new JButton("Argentina");
JButton CaliforniaButton=new JButton("California");
JButton ChileButton=new JButton("Chile");
JButton EspanaButton=new JButton("Espana");
JButton FranciaButton=new JButton("Francia");
JButton ItaliaButton=new JButton("Italia");
JButton NZButton=new JButton("New Zealand");
JButton PortugalButton=new JButton("Portugal");
JButton PRButton=new JButton("Puerto Rico");
JButton USAButton=new JButton("USA");
JScrollPane scroll=new
ScrollPane(Title,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
public locationFrame()
{
super("Wine Application");
setLayout(null);
Title.setFont(tFont);
Title.setLocation(450,0);
Title.setSize(300,100);
label.setIcon(new ImageIcon(img));
label.setBounds(350,100,500,500);
backButton.setFont(bFont);
backButton.setBounds(800,1800,400,100);
AlemaniButton.setFont(bFont);
ArgentinaButton.setFont(bFont);
CaliforniaButton.setFont(bFont);
ChileButton.setFont(bFont);
EspanaButton.setFont(bFont);
FranciaButton.setFont(bFont);
ItaliaButton.setFont(bFont);
NZButton.setFont(bFont);
PortugalButton.setFont(bFont);
PRButton.setFont(bFont);
USAButton.setFont(bFont);
AlemaniButton.setBounds(300,700,600,150);
ArgentinaButton.setBounds(300,1085,600,150);
CaliforniaButton.setBounds(300,1470,600,150);
ChileButton.setBounds(300,1855,600,150);
EspanaButton.setBounds(300,2240,600,150);
FranciaButton.setBounds(300,2625,600,150);
ItaliaButton.setBounds(300,3010,600,150);
NZButton.setBounds(300,3395,600,150);
PortugalButton.setBounds(300,3780,600,150);
PRButton.setBounds(300,4165,600,150);
USAButton.setBounds(300,4550,600,150);
setExtendedState(JFrame.MAXIMIZED_BOTH);
add(Title);
add(label);
add(backButton);
add(AlemaniButton);
add(ArgentinaButton);
add(CaliforniaButton);
add(ChileButton);
add(EspanaButton);
add(FranciaButton);
add(ItaliaButton);
add(NZButton);
add(PortugalButton);
add(PRButton);
add(USAButton);
add(scroll);
getContentPane().setBackground(new Color(255,255,255));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
backButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==backButton)
{
mainWineFrame mainFrame=new mainWineFrame();
mainFrame.setVisible(true);
setVisible(false);
}
}
}
感谢您的反馈意见。你是对的。我应该使用适当的代码格式,如camalCase来简化事情。代码: // JScrollPane scroll = new JScrollPane(Title,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);和/ /添加(滚动);进行测试,看看自从我尝试使用JScrollPane后会发生什么。创建一个同时具有JScrollpane和所有按钮的面板,然后将该面板添加到我的JFrame中会更容易吗? –
没问题,很高兴我能帮到你。不过,关于您的问题,是的,我相信如果将所有按钮添加到JPanel并将该JPanel设置为JScrollPane的视口,会更好。之后,您应该将JScrollPane添加到您的JFrame中,一切都会正常工作。我还在答案中列入了一些我认为接近你想达到的结果。 – fwerther