[英]Referencing an object from another class
I've been fiddling with TabbedPanes, and I wish to be able to use a method in another class to set which tab is selected. 我一直在摆弄TabbedPanes,并且希望能够使用另一个类中的方法来设置选择哪个标签。
However, when attempting to do this, I am provided with two errors. 但是,尝试执行此操作时,出现两个错误。
First, it tells me that the parameter 'name' that I have provided the method in the second class has 'private access in Component' 首先,它告诉我第二类中提供的方法的参数“名称”具有“组件中的私有访问权限”
error: name has private access in Component
TabbedPanes.name.setSelectedIndex(0);
Second, it tells me that it cannot find the symbol for the method I wish to call from within the second class' custom method. 其次,它告诉我无法从第二个类的自定义方法中找到我要调用的方法的符号。
error: cannot find symbol
TabbedPanes.name.setSelectedIndex(0);
^
symbol: method setSelectedIndex(int)
location: variable name of type String
My first class, TabbedPanes, can be found below: 我的第一堂课TabbedPanes可以在下面找到:
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class TabbedPanes extends JFrame implements ActionListener
{
JTabbedPane tabs;
JPanel pan1, pan2, pan3, pan4, pan5, pan6, pan7, pan8;
JFrame frame;
JScrollPane scroll1, scroll2, scroll3, scroll4, scroll5, scroll6, scroll7, scroll8;
public void Tabs()
{
tabs = new JTabbedPane();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JScrollPane scroll1 = new JScrollPane(pan1);
scroll1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
pan2 = new JPanel();
pan3 = new JPanel();
pan4 = new JPanel();
pan5 = new JPanel();
pan6 = new JPanel();
pan7 = new JPanel();
pan8 = new JPanel();
JScrollPane scroll8 = new JScrollPane(pan8);
scroll8.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll8.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
Container pane = frame.getContentPane();
pane.add(tabs);
tabs.add("Mon", scroll1);
tabs.add("Tue", pan2);
tabs.add("Wed", pan3);
tabs.add("Thu", pan4);
tabs.add("Fri", pan5);
tabs.add("Sat", pan6);
tabs.add("Sun", pan7);
tabs.add("Notes", scroll8);
Dates datesObject = new Dates();
datesObject.tabOnStartup("tabs");
frame.setSize(400,400);
frame.setVisible(true);
}
public static void main(String[] args)
{
TabbedPanes TrueTabs = new TabbedPanes();
TrueTabs.Tabs();
}
}
The second class, Dates, can be found below: 第二类,日期,可以在下面找到:
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class Dates
{
public void tabOnStartup(String name)
{
TabbedPanes.name.setSelectedIndex(0);
}
Any help will be greatly appreciated, and I hope my code is clean enough to read well. 任何帮助将不胜感激,我希望我的代码足够干净可以阅读。
If I got you right, you have to change the variable JTabbedPane tabs in class TabbedPanes to static: static JTabbedPane tabs;
如果我有你的权利,你必须改变类TabbedPanes变量JTabbedPane的选项卡,静态:
static JTabbedPane tabs;
Also in class Dates you have to use 此外,在课堂上您必须使用日期
TabbedPanes.tabs.setSelectedIndex(0);
instead of 代替
TabbedPanes.name.setSelectedIndex(0);
It seems that you have not implemented a method in class TabbedPanes : 看来您尚未在TabbedPanes类中实现方法:
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
If you are using IDE like Eclipse, NetBeans, etc. you should've received an error message like 如果您正在使用IDE(例如Eclipse,NetBeans等),则应该收到一条错误消息,例如
- The type TabbedPanes must implement the inherited abstract method
ActionListener.actionPerformed(ActionEvent)
After these ammendments the result is: 经过这些修正后,结果是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.